Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 3 files changed
  • 0 comments
  • 1 contributor
34  src/nooz/crypto.clj
@@ -9,7 +9,8 @@
9 9
            java.security.SecureRandom
10 10
            java.security.MessageDigest
11 11
            java.math.BigInteger
12  
-           java.util.Random))
  12
+           java.util.Random
  13
+           java.nio.ByteBuffer))
13 14
 
14 15
 (def ^{:private true
15 16
        :doc "Algorithm to seed random numbers."}
@@ -19,18 +20,6 @@
19 20
 (def ^{:private true} idkey "nextid")
20 21
 (def ^{:private true} epochStart 1387263000)
21 22
 
22  
-(defn gen-id
23  
-  "Generate a random unique ID."
24  
-  ([]
25  
-     (let [ms (- (nt/long-now) epochStart)
26  
-           r (.nextLong rando)
27  
-           id (mod (redis/with-server db/prod-redis (redis/incr idkey))
28  
-                   1024)]
29  
-       (bit-or
30  
-        (bit-or (bit-shift-left (bit-and ms 0x000001FFFFFFFFFF) 23)
31  
-                (bit-shift-left (bit-and r  0x0000000000001FFF) 10))
32  
-        (bit-and id 0x00000000000003FF)))))
33  
-
34 23
 (defn gen-salt
35 24
   "Generate a salt for BCrypt's hashing routines."
36 25
   ([]
@@ -74,3 +63,22 @@
74 63
 (defn gen-confirmation-token []
75 64
   (let [hashbytes (gen-random-hash 16)]
76 65
     (wrap-web-safe (codec/base64-encode hashbytes))))
  66
+
  67
+(defn long-to-web-safe [val]
  68
+  (let [b (. ByteBuffer allocate 8)]
  69
+    (.putLong b val)
  70
+    (wrap-web-safe (codec/base64-encode (.array b)))))
  71
+
  72
+(defn gen-id
  73
+  "Generate a random unique ID."
  74
+  ([]
  75
+     (let [ms (- (nt/long-now) epochStart)
  76
+           r  (.nextLong rando)
  77
+           id (mod (redis/with-server db/prod-redis
  78
+                     (redis/incr idkey))
  79
+                   1024)]
  80
+       (long-to-web-safe
  81
+        (bit-or
  82
+         (bit-or (bit-shift-left (bit-and ms 0x000001FFFFFFFFFF) 23)
  83
+                 (bit-shift-left (bit-and r  0x0000000000001FFF) 10))
  84
+         (bit-and id 0x00000000000003FF))))))
4  src/nooz/models/comment.clj
@@ -46,8 +46,8 @@
46 46
     (redis/with-server db/prod-redis
47 47
       (record-comment! post comment)
48 48
       (update-comment-count! (get post "id"))
49  
-      (record-comment-for-user! comment (get post "id") user time)
50  
-      (get comment "id"))))
  49
+      (record-comment-for-user! comment (get post "id") user time))
  50
+    (get comment "id")))
51 51
 
52 52
 (defn create-comment! [post user {:keys [parent comment] :as args}]
53 53
   (if (valid-comment? args)
2  src/nooz/server.clj
@@ -38,4 +38,4 @@
38 38
   (-main :dev)
39 39
   server-config
40 40
   (swap! server-config assoc :port 8081)
41  
-  )
  41
+  )

No commit comments for this range

Something went wrong with that request. Please try again.