浏览代码

core/crypto/blake2: Add the ability to easily alter digest size

Yawning Angel 10 月之前
父节点
当前提交
dc94452fb9
共有 2 个文件被更改,包括 12 次插入6 次删除
  1. 6 3
      core/crypto/blake2b/blake2b.odin
  2. 6 3
      core/crypto/blake2s/blake2s.odin

+ 6 - 3
core/crypto/blake2b/blake2b.odin

@@ -18,7 +18,7 @@ package blake2b
 import "../_blake2"
 import "../_blake2"
 
 
 // DIGEST_SIZE is the BLAKE2b digest size in bytes.
 // DIGEST_SIZE is the BLAKE2b digest size in bytes.
-DIGEST_SIZE :: 64
+DIGEST_SIZE :: _blake2.BLAKE2B_SIZE
 
 
 // BLOCK_SIZE is the BLAKE2b block size in bytes.
 // BLOCK_SIZE is the BLAKE2b block size in bytes.
 BLOCK_SIZE :: _blake2.BLAKE2B_BLOCK_SIZE
 BLOCK_SIZE :: _blake2.BLAKE2B_BLOCK_SIZE
@@ -27,9 +27,12 @@ BLOCK_SIZE :: _blake2.BLAKE2B_BLOCK_SIZE
 Context :: _blake2.Blake2b_Context
 Context :: _blake2.Blake2b_Context
 
 
 // init initializes a Context with the default BLAKE2b config.
 // init initializes a Context with the default BLAKE2b config.
-init :: proc(ctx: ^Context) {
+init :: proc(ctx: ^Context, digest_size := DIGEST_SIZE) {
+	if digest_size > 255 {
+		panic("blake2b: invalid digest size")
+	}
 	cfg: _blake2.Blake2_Config
 	cfg: _blake2.Blake2_Config
-	cfg.size = _blake2.BLAKE2B_SIZE
+	cfg.size = u8(digest_size)
 	_blake2.init(ctx, &cfg)
 	_blake2.init(ctx, &cfg)
 }
 }
 
 

+ 6 - 3
core/crypto/blake2s/blake2s.odin

@@ -18,7 +18,7 @@ package blake2s
 import "../_blake2"
 import "../_blake2"
 
 
 // DIGEST_SIZE is the BLAKE2s digest size in bytes.
 // DIGEST_SIZE is the BLAKE2s digest size in bytes.
-DIGEST_SIZE :: 32
+DIGEST_SIZE :: _blake2.BLAKE2S_SIZE
 
 
 // BLOCK_SIZE is the BLAKE2s block size in bytes.
 // BLOCK_SIZE is the BLAKE2s block size in bytes.
 BLOCK_SIZE :: _blake2.BLAKE2S_BLOCK_SIZE
 BLOCK_SIZE :: _blake2.BLAKE2S_BLOCK_SIZE
@@ -27,9 +27,12 @@ BLOCK_SIZE :: _blake2.BLAKE2S_BLOCK_SIZE
 Context :: _blake2.Blake2s_Context
 Context :: _blake2.Blake2s_Context
 
 
 // init initializes a Context with the default BLAKE2s config.
 // init initializes a Context with the default BLAKE2s config.
-init :: proc(ctx: ^Context) {
+init :: proc(ctx: ^Context, digest_size := DIGEST_SIZE) {
+	if digest_size > 255 {
+		panic("blake2s: invalid digest size")
+	}
 	cfg: _blake2.Blake2_Config
 	cfg: _blake2.Blake2_Config
-	cfg.size = _blake2.BLAKE2S_SIZE
+	cfg.size = u8(digest_size)
 	_blake2.init(ctx, &cfg)
 	_blake2.init(ctx, &cfg)
 }
 }