Back
->bytes (clj)
(source)function
(->bytes data)
A convenience alias for to-bytes
Examples
funcool/buddy-core
(ns buddy.core.kdf-tests
(:require [clojure.test :refer :all]
[buddy.core.codecs :as c]
[buddy.core.bytes :as b]
[buddy.core.keys :as k]
[buddy.core.nonce :as n]
[buddy.core.kdf :as kdf]
[buddy.core.hash :as hash]
[clojure.java.io :as io]))
(deftest hkdf-sha256-test
(let [key (c/hex->bytes "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b")
salt (c/hex->bytes "000102030405060708090a0b0c")
info (c/hex->bytes "f0f1f2f3f4f5f6f7f8f9")
expected (c/hex->bytes (str "3cb25f25faacd57a90434f64d0362f2a"
"2d2d0a90cf1a5a4c5db02d56ecc4c5bf"
"34007208d5b887185865"))
engine (kdf/engine {:key key :salt salt :info info :alg :hkdf+sha256})
result (kdf/get-bytes engine 42)]
;; (println (c/bytes->hex result))
;; (println (c/bytes->hex expected))
(is (b/equals? result expected))))
(deftest kdf1-sha1-test
(let [key (c/hex->bytes "deadbeeffeebdaed")
engine (kdf/engine {:key key :alg :kdf1 :digest :sha1})
expected (c/hex->bytes (str "B0AD565B14B478CAD4763856FF3016B1A93D840"
"F87261BEDE7DDF0F9305A6E44"))
result (kdf/get-bytes engine 32)]
;; (println (c/bytes->hex result))
;; (println (c/bytes->hex expected))
(is (b/equals? result expected))))
(deftest kdf2-sha1-test
(let [key (c/hex->bytes "deadbeeffeebdaed")
engine (kdf/engine {:key key :alg :kdf2 :digest :sha1})
expected (c/hex->bytes (str "87261BEDE7DDF0F9305A6E44A74E6A0846DEDE27"
"F48205C6B141888742B0CE2C"))
result (kdf/get-bytes engine 32)]
;; (println (c/bytes->hex result))
;; (println (c/bytes->hex expected))
(is (b/equals? result expected))))
(deftest pbkdf2-sha1-test-1
(let [key (c/str->bytes "password")
salt (c/str->bytes "salt")
expected (c/hex->bytes "0C60C80F961F0E71F3A9B524AF6012062FE037A6")
engine (kdf/engine {:key key :salt salt :alg :pbkdf2 :digest :sha1
:iterations 1})
result (kdf/get-bytes engine 20)]
(is (b/equals? result expected))))
(deftest pbkdf2-sha1-test-2
(let [key (c/str->bytes "password")
salt (c/str->bytes "salt")
expected (c/hex->bytes "EA6C014DC72D6F8CCD1ED92ACE1D41F0D8DE8957")
engine (kdf/engine {:key key :salt salt :alg :pbkdf2 :digest :sha1
:iterations 2})
result (kdf/get-bytes engine 20)]
(is (b/equals? result expected))))
(deftest pbkdf2-sha1-test-4096
(let [key (c/str->bytes "password")
salt (c/str->bytes "salt")
expected (c/hex->bytes "4B007901B765489ABEAD49D926F721D065A429C1")
engine (kdf/engine {:key key :salt salt :alg :pbkdf2 :digest :sha1
:iterations 4096})
result (kdf/get-bytes engine 20)]
(is (b/equals? result expected))))
(deftest pbkdf2-sha1-test-16777216
(let [key (c/str->bytes "password")
salt (c/str->bytes "salt")
expected (c/hex->bytes "EEFE3D61CD4DA4E4E9945B3D6BA2158C2634E984")
engine (kdf/engine {:key key :salt salt :alg :pbkdf2 :digest :sha1
:iterations 16777216})
result (kdf/get-bytes engine 20)]
(is (b/equals? result expected))))
(deftest pbkdf2-sha1-test-long-pass
(let [key (c/str->bytes "passwordPASSWORDpassword")
salt (c/str->bytes "saltSALTsaltSALTsaltSALTsaltSALTsalt")
expected (c/hex->bytes (str "3D2EEC4FE41C849B80C8D83662C0E44A8B291A964C"
"F2F07038"))
engine (kdf/engine {:key key :salt salt :alg :pbkdf2 :digest :sha1
:iterations 4096})
result (kdf/get-bytes engine 25)]
(is (b/equals? result expected))))
(deftest pbkdf2-sha1-test-binary
(let [key (c/str->bytes "pass\000word")
salt (c/str->bytes "sa\000lt")
expected (c/hex->bytes "56FA6AA75548099DCC37D7F03425E0C3")
engine (kdf/engine {:key key :salt salt :alg :pbkdf2 :digest :sha1
:iterations 4096})
result (kdf/get-bytes engine 16)]
(is (b/equals? result expected))))
funcool/buddy-core
(ns buddy.core.codecs-tests
(:require [clojure.test :refer :all]
[buddy.core.codecs :as codecs :refer :all]
[buddy.core.codecs.base64 :as b64]
[buddy.core.bytes :as bytes]
[buddy.core.keys :refer :all]
[buddy.core.hash :as hash]
[clojure.java.io :as io]))
(deftest buddy-core-codecs
(testing "Hex encode/decode 01"
(let [some-bytes (str->bytes "FooBar")
encoded (bytes->hex some-bytes)
decoded (hex->bytes encoded)
some-str (bytes->str decoded)]
(is (bytes/equals? decoded, some-bytes))
(is (= some-str "FooBar"))))
(testing "Hex encode/decode 02"
(let [mybytes (into-array Byte/TYPE (range 10))
encoded (bytes->hex mybytes)
decoded (hex->bytes encoded)]
(is (bytes/equals? decoded mybytes))))
(deftest buddy-core-codecs-2
(testing "b64 1"
(let [mybytes (into-array Byte/TYPE (range 10))
encoded (bytes->b64 mybytes)
decoded (b64->bytes encoded)]
(is (bytes/equals? decoded mybytes))))
(testing "b64 2"
(let [mydata "hello world"
encoded (-> (str->bytes mydata) (bytes->b64))
decoded (b64->str encoded)]
(is (= decoded mydata))))
(testing "b64 3"
(let [mydata "hello world2"
encoded (-> (str->bytes mydata) (bytes->b64 true))
decoded (b64->str encoded false)]
(is (= decoded mydata))))
)
funcool/buddy-core
(ns buddy.core.crypto-tests
(:require [clojure.test :refer :all]
[clojure.pprint :refer :all]
[buddy.core.codecs :as codecs :refer :all]
[buddy.core.bytes :as bytes]
[buddy.core.keys :refer :all]
[buddy.core.nonce :as nonce]
[buddy.core.hash :as hash]
[buddy.core.crypto :as crypto]
[clojure.java.io :as io]))
(deftest low-level-api-tests
(let [key (nonce/random-bytes 32)
iv (nonce/random-bytes 16)
key (hex->bytes "0000000000000000000000000000000000000000000000000000000000000000")
iv16 (hex->bytes "00000000000000000000000000000000")
iv8 (hex->bytes "0011001100110011")
block16 (hex->bytes "000000000000000000000000000000AA")
block3 (hex->bytes "121314")
block6 (hex->bytes "221122112211")]
(deftest low-level-aead-mode-tests
(let [key (hex->bytes "0000000000000000000000000000000000000000000000000000000000000000")
data (nonce/random-bytes 256)
iv16 (nonce/random-bytes 16)
cipher (crypto/block-cipher :aes :gcm)]
(crypto/initialize! cipher {:iv iv16 :key key :op :encrypt})
(let [finalsize (crypto/get-output-size cipher (count data))]
(is (= finalsize 272)))
(let [output (byte-array 272)
offset (crypto/process-block! cipher data 0 output 0)
offset' (crypto/end! cipher output offset)]
(crypto/initialize! cipher {:iv iv16 :key key :op :decrypt})
(let [input output
finalsize (crypto/get-output-size cipher (count input))]
(is (= finalsize 256))
(let [output (byte-array 256)
offset (crypto/process-block! cipher input 0 output 0)
offset' (crypto/end! cipher output offset)]
(is (bytes/equals? output data)))))))
funcool/buddy-core
(ns buddy.core.mac-tests
(:require [clojure.test :refer :all]
[buddy.core.codecs :as codecs :refer :all]
[buddy.core.bytes :as bytes]
[buddy.core.keys :refer :all]
[buddy.core.nonce :as nonce]
(testing "Test Vector"
(let [key (hex->bytes (str "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))
data (str->bytes "Test Using Larger Than Block-Size Key - Hash Key First")
result "60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54"]
(is (= result (bytes->hex (mac/hash data {:key key :alg :hmac+sha256}))))
(is (= result (bytes->hex (mac/hash data {:key key :alg :hmac :digest :sha256}))))
(is (= result (bytes->hex (mac/hash data {:key key :alg :hmac :digest (SHA256Digest.)}))))))
funcool/buddy-core
(ns buddy.core.hash-tests
(:require [clojure.test :refer :all]
[clojure.string :as str]
[buddy.core.codecs :as codecs :refer :all]
[buddy.core.bytes :as bytes]
[buddy.core.keys :refer :all]
[buddy.core.hash :as hash]
[clojure.java.io :as io]))
(testing "low-level api"
(let [engine (org.bouncycastle.crypto.digests.SHA1Digest.)]
(hash/update! engine (str->bytes ""))
(let [data (hash/end! engine)]
(is (= (bytes->hex data)
"da39a3ee5e6b4b0d3255bfef95601890afd80709"))))))