implementing a crossover hash function
This commit is contained in:
@@ -6,3 +6,4 @@ pom.xml
|
|||||||
/classes/
|
/classes/
|
||||||
.lein-*
|
.lein-*
|
||||||
resources/public/js/*
|
resources/public/js/*
|
||||||
|
.crossover-cljs
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
:source-path "src-cljs"
|
:source-path "src-cljs"
|
||||||
; The standard ClojureScript compiler options:
|
; The standard ClojureScript compiler options:
|
||||||
; (See the ClojureScript compiler documentation for details.)
|
; (See the ClojureScript compiler documentation for details.)
|
||||||
|
:crossovers [NoteHub.crossover]
|
||||||
:compiler {
|
:compiler {
|
||||||
:output-dir "resources/public/cljs/"
|
:output-dir "resources/public/cljs/"
|
||||||
:output-to "resources/public/cljs/main.js" ; default: main.js in current directory
|
:output-to "resources/public/cljs/main.js" ; default: main.js in current directory
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
(:use [jayq.core :only [$ css inner val anim show]])
|
(:use [jayq.core :only [$ css inner val anim show]])
|
||||||
(:require [fetch.remotes :as remotes]
|
(:require [fetch.remotes :as remotes]
|
||||||
[goog.dom :as gdom]
|
[goog.dom :as gdom]
|
||||||
|
[NoteHub.crossover.lib :as nh]
|
||||||
[clojure.browser.dom :as dom]
|
[clojure.browser.dom :as dom]
|
||||||
[clojure.browser.event :as event])
|
[clojure.browser.event :as event])
|
||||||
(:require-macros [fetch.macros :as fm]))
|
(:require-macros [fetch.macros :as fm]))
|
||||||
@@ -33,3 +34,8 @@
|
|||||||
(show $preview-start-line)
|
(show $preview-start-line)
|
||||||
(inner $preview result)
|
(inner $preview result)
|
||||||
(scroll-to $preview-start-line)))))
|
(scroll-to $preview-start-line)))))
|
||||||
|
|
||||||
|
(.click ($ :#publish-button)
|
||||||
|
(fn [e]
|
||||||
|
(val ($ :#session-value)
|
||||||
|
(nh/hash #(.charCodeAt % 0) (str (val $draft) (val ($ :#session-key)))))))
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
(ns NoteHub.crossover.lib
|
||||||
|
(:refer-clojure :exclude [hash]))
|
||||||
|
|
||||||
|
; very simple hash function %)
|
||||||
|
; (doesn't work for UTF-16!)
|
||||||
|
(defn hash [f s]
|
||||||
|
(let [short-mod #(mod % 32767)
|
||||||
|
; we cannot use Math/pow because it's imprecise
|
||||||
|
; and differs on JVM and JS
|
||||||
|
pow (fn [n e]
|
||||||
|
(reduce #(short-mod (* % %2)) 1
|
||||||
|
(repeat e n)))
|
||||||
|
char-codes (map #(f (str %)) s)]
|
||||||
|
(reduce
|
||||||
|
#(short-mod (+ %
|
||||||
|
(short-mod (* (first %2) (pow 31 (second %2))))))
|
||||||
|
0 (map list char-codes (range)))))
|
||||||
+31
-10
@@ -1,12 +1,15 @@
|
|||||||
(ns NoteHub.views.pages
|
(ns NoteHub.views.pages
|
||||||
(:require [NoteHub.views.common :as common])
|
(:require [NoteHub.views.common :as common])
|
||||||
|
(:require [NoteHub.crossover.lib :as nh])
|
||||||
(:use
|
(:use
|
||||||
[NoteHub.storage]
|
[NoteHub.storage]
|
||||||
[clojure.string :rename {replace sreplace} :only [split replace lower-case]]
|
[clojure.string :rename {replace sreplace} :only [split replace lower-case]]
|
||||||
[clojure.core.incubator :only [-?>]]
|
[clojure.core.incubator :only [-?>]]
|
||||||
[hiccup.form]
|
[hiccup.form]
|
||||||
|
[noir.session :only [flash-put! flash-get]]
|
||||||
[noir.response :only [redirect]]
|
[noir.response :only [redirect]]
|
||||||
[noir.core :only [defpage render]]
|
[noir.core :only [defpage render]]
|
||||||
|
[noir.util.crypt :only [encrypt]]
|
||||||
[noir.statuses]
|
[noir.statuses]
|
||||||
[noir.fetch.remotes])
|
[noir.fetch.remotes])
|
||||||
(:import
|
(:import
|
||||||
@@ -20,13 +23,16 @@
|
|||||||
(defremote md-to-html [draft]
|
(defremote md-to-html [draft]
|
||||||
(.markdownToHtml (PegDownProcessor.) draft))
|
(.markdownToHtml (PegDownProcessor.) draft))
|
||||||
|
|
||||||
; Template for the 404 error
|
; Template for the error sites
|
||||||
(set-page! 404
|
(defn page-setter [code message]
|
||||||
(let [message "Page Not Found."]
|
(set-page! code
|
||||||
(common/layout message
|
(common/layout message
|
||||||
[:article
|
[:article
|
||||||
[:h1 message]])))
|
[:h1 message]])))
|
||||||
|
|
||||||
|
(page-setter 404 "Nothing Found.")
|
||||||
|
(page-setter 400 "Bad request.")
|
||||||
|
|
||||||
; Routes
|
; Routes
|
||||||
; ======
|
; ======
|
||||||
|
|
||||||
@@ -44,9 +50,12 @@
|
|||||||
(common/layout "New Markdown Note"
|
(common/layout "New Markdown Note"
|
||||||
[:div.central-element
|
[:div.central-element
|
||||||
(form-to [:post "/post-note"]
|
(form-to [:post "/post-note"]
|
||||||
(text-area {:class "max-width"} :draft)
|
(hidden-field :session-key (let [k (encrypt (str (rand-int Integer/MAX_VALUE)))]
|
||||||
|
(do (flash-put! k true) (print-str k))))
|
||||||
|
(hidden-field {:id :session-value} :session-value)
|
||||||
|
(text-area {:class :max-width} :draft)
|
||||||
[:div#buttons.hidden
|
[:div#buttons.hidden
|
||||||
(submit-button {:style "float: left" :class "button"} "Publish")
|
(submit-button {:style "float: left" :class :button :id :publish-button} "Publish")
|
||||||
[:button#preview-button.button {:type :button :style "float: right"} "Preview"]])]
|
[:button#preview-button.button {:type :button :style "float: right"} "Preview"]])]
|
||||||
[:div#preview-start-line.hidden]
|
[:div#preview-start-line.hidden]
|
||||||
[:article#preview]))
|
[:article#preview]))
|
||||||
@@ -60,10 +69,10 @@
|
|||||||
(common/layout title
|
(common/layout title
|
||||||
[:article
|
[:article
|
||||||
(md-to-html post)])
|
(md-to-html post)])
|
||||||
(get-page 400))))
|
(get-page 404))))
|
||||||
|
|
||||||
; New Note Posting
|
; New Note Posting
|
||||||
(defpage [:post "/post-note"] {:keys [draft]}
|
(defpage [:post "/post-note"] {:keys [draft session-key session-value]}
|
||||||
(let [[year month day] (map #(+ (second %) (.get (Calendar/getInstance) (first %)))
|
(let [[year month day] (map #(+ (second %) (.get (Calendar/getInstance) (first %)))
|
||||||
{Calendar/YEAR 0, Calendar/MONTH 1, Calendar/DAY_OF_MONTH 0})
|
{Calendar/YEAR 0, Calendar/MONTH 1, Calendar/DAY_OF_MONTH 0})
|
||||||
untrimmed-line (filter #(or (= \- %) (Character/isLetterOrDigit %))
|
untrimmed-line (filter #(or (= \- %) (Character/isLetterOrDigit %))
|
||||||
@@ -75,6 +84,18 @@
|
|||||||
title (first (drop-while #(note-exists? date %)
|
title (first (drop-while #(note-exists? date %)
|
||||||
(cons proposed-title
|
(cons proposed-title
|
||||||
(map #(str proposed-title "-" (+ 2 %)) (range)))))]
|
(map #(str proposed-title "-" (+ 2 %)) (range)))))]
|
||||||
(do
|
; check whether the new note can be added
|
||||||
(set-note date title draft)
|
(let [valid-session (flash-get session-key) ; it was posted from a newly generated form
|
||||||
(redirect (apply str (interpose "/" ["" year month day title]))))))
|
valid-draft (not (empty? draft)) ; the note is non-empty
|
||||||
|
valid-hash (= (Short/parseShort session-value) ; the hash code is correct
|
||||||
|
(nh/hash #(.codePointAt % 0) (str draft session-key)))]
|
||||||
|
(do
|
||||||
|
(println "session:" valid-session "draft:" valid-draft "hash:"
|
||||||
|
(Short/parseShort session-value)
|
||||||
|
(nh/hash #(.codePointAt % 0) (str draft session-key)))
|
||||||
|
(if (and valid-session valid-draft valid-hash)
|
||||||
|
(do
|
||||||
|
(set-note date title draft)
|
||||||
|
; TODO: the redirect is broken if title contains UTF chars
|
||||||
|
(redirect (apply str (interpose "/" ["" year month day title]))))
|
||||||
|
(get-page 400))))))
|
||||||
|
|||||||
Reference in New Issue
Block a user