diff --git a/doc/01-hash.md b/doc/01-hash.md index 113131b..f5f0faa 100644 --- a/doc/01-hash.md +++ b/doc/01-hash.md @@ -4,21 +4,21 @@ All hash algorithms are located in the `buddy.core.hash` namespace. Available hash algorithms: -| Hash algorithm name | Digest size | -| --- |---| -| SHA1 | 160 | -| SHA2 | 256, 384, 512 | -| SHA3 | 256, 384, 512 | -| MD5 | 128 | -| Tiger | 192 | -| Blake2b | 512 | -| Skein | 256, 512, 1024, arbitrary size | -| Whirlpool | 512 | -| RIPEMD128 | 128 | -| RIPEMD160 | 160 | -| RIPEMD256 | 256 | -| RIPEMD320 | 320 | - +| Hash algorithm name | Digest size | +|---------------------|--------------------------------| +| SHA1 | 160 | +| SHA2 | 256, 384, 512 | +| SHA3 | 256, 384, 512 | +| MD5 | 128 | +| Tiger | 192 | +| BLAKE2b | 128, 256, 512 | +| BLAKE3 | 256, arbitrary size | +| Skein | 256, 512, 1024, arbitrary size | +| Whirlpool | 512 | +| RIPEMD128 | 128 | +| RIPEMD160 | 160 | +| RIPEMD256 | 256 | +| RIPEMD320 | 320 | ## Basic usage diff --git a/src/buddy/core/hash.clj b/src/buddy/core/hash.clj index a01c67c..13ee8e2 100644 --- a/src/buddy/core/hash.clj +++ b/src/buddy/core/hash.clj @@ -32,31 +32,33 @@ org.bouncycastle.crypto.digests.SHA384Digest org.bouncycastle.crypto.digests.SHA512Digest org.bouncycastle.crypto.digests.Blake2bDigest + org.bouncycastle.crypto.digests.Blake3Digest org.bouncycastle.crypto.digests.SkeinDigest org.bouncycastle.crypto.digests.WhirlpoolDigest)) (def ^:no-doc ^:static +digest-engines+ - {:sha256 #(SHA256Digest.) - :sha384 #(SHA384Digest.) - :sha512 #(SHA512Digest.) - :sha1 #(SHA1Digest.) - :ripemd128 #(RIPEMD128Digest.) - :ripemd160 #(RIPEMD160Digest.) - :ripemd256 #(RIPEMD256Digest.) - :ripemd320 #(RIPEMD320Digest.) - :tiger #(TigerDigest.) - :md5 #(MD5Digest.) - :sha3-256 #(SHA3Digest. 256) - :sha3-384 #(SHA3Digest. 384) - :sha3-512 #(SHA3Digest. 512) + {:sha256 #(SHA256Digest.) + :sha384 #(SHA384Digest.) + :sha512 #(SHA512Digest.) + :sha1 #(SHA1Digest.) + :ripemd128 #(RIPEMD128Digest.) + :ripemd160 #(RIPEMD160Digest.) + :ripemd256 #(RIPEMD256Digest.) + :ripemd320 #(RIPEMD320Digest.) + :tiger #(TigerDigest.) + :md5 #(MD5Digest.) + :sha3-256 #(SHA3Digest. 256) + :sha3-384 #(SHA3Digest. 384) + :sha3-512 #(SHA3Digest. 512) :blake2b-128 #(Blake2bDigest. nil 16 nil nil) :blake2b-256 #(Blake2bDigest. nil 32 nil nil) :blake2b-512 #(Blake2bDigest. nil 64 nil nil) - :skein-256 #(SkeinDigest. 256 256) - :skein-512 #(SkeinDigest. 512 512) - :skein-1024 #(SkeinDigest. 1024 1024) - :whirlpool #(WhirlpoolDigest.)}) + :blake3-256 #(Blake3Digest. 256) + :skein-256 #(SkeinDigest. 256 256) + :skein-512 #(SkeinDigest. 512 512) + :skein-1024 #(SkeinDigest. 1024 1024) + :whirlpool #(WhirlpoolDigest.)}) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Protocol definitions (abstractions) @@ -196,6 +198,20 @@ [input] (digest input :blake2b-512)) +(defn blake3 + "BLAKE3 is a cryptographic hash function faster than MD5, + SHA-1, SHA-2, SHA-3, and BLAKE2. It focuses on speed." + [input length] + (let [length (* length 8) + engine (Blake3Digest. ^int length)] + (-digest input engine))) + +(defn blake3-256 + "BLAKE3 cryptographic hash function with fixed output + digest size to 256 bits." + [input] + (digest input :blake3-256)) + (defn skein "Skein is a cryptographic hash function based on Threefish tweakable block cipher compressed using diff --git a/test/buddy/core/hash_tests.clj b/test/buddy/core/hash_tests.clj index 6b33892..d0a15ab 100644 --- a/test/buddy/core/hash_tests.clj +++ b/test/buddy/core/hash_tests.clj @@ -76,6 +76,13 @@ "D25E1031AFEE585313896444934EB04B" "903A685B1448B755D56F701AFE9BE2CE"))))) + (testing "blake3 256" + (let [hashed1 (hash/blake3-256 "") + hashed2 (hash/digest "" :blake3-256)] + (is (bytes/equals? hashed1 hashed2)) + (is (= (bytes->hex hashed1) + "af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262")))) + (testing "skein 256" (let [hashed1 (hash/skein-256 "") hashed2 (hash/skein "" 32)