Skip to content

Commit

Permalink
Merge pull request #83 from hansenpansen/feature/blake3
Browse files Browse the repository at this point in the history
Add BLAKE3 hash through BC Blake3Digest.
  • Loading branch information
niwinz authored Jul 19, 2024
2 parents cac99da + 5699e05 commit 9502c56
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 32 deletions.
30 changes: 15 additions & 15 deletions doc/01-hash.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
50 changes: 33 additions & 17 deletions src/buddy/core/hash.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions test/buddy/core/hash_tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9502c56

Please sign in to comment.