Commits

yongchao li  committed 7d7dd4b

add new file

  • Participants
  • Parent commits 8bc6dc5

Comments (0)

Files changed (2)

File src/clj_test/Cryptography.clj

+(ns clj_test.Cryptography)
+(defn rand-bytes [size]
+  (let [rand (java.security.SecureRandom/getInstance "SHA1PRNG")
+        buffer (make-array Byte/TYPE size)]
+    (.nextBytes rand buffer)
+    buffer))
+
+(defn encrypt [m]
+  (let [message (.getBytes m)
+        size (count message)
+        pad (rand-bytes size)
+        code (map bit-xor message pad)]
+    {:pad (vec pad) :msg (vec code)}))
+
+(encrypt "Attack At Down")

File src/clj_test/ref_test.clj

+(ns clj_test.ref-test)
+(import 'java.util.Random)
+
+(def N1 (ref 0))
+(def N2 (ref 1))
+
+(defn writer []
+  (let [n (.nextInt (Random.))]
+    (dosync (ref-set N1 n)
+            (ref-set N2 (inc n)))
+    (println "writer ...")
+    (Thread/sleep 100)
+    (recur)))
+
+(defn reader []
+  (if (not (= 1 (dosync (- @N2 @N1))))
+    (println "______________________");never print
+    (do
+      (println "reader ...")
+      (Thread/sleep 100)
+      (recur))))
+
+(defn start-all-threads []
+  (let [t1 (Thread. writer)
+        t2 (Thread. reader)]
+    (.start t1)
+    (.start t2)
+    (.join t1)
+    (.join t2)))
+
+(start-all-threads)