We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5401eae commit 6f9d78bCopy full SHA for 6f9d78b
modules/crypto/hmac.nu
@@ -0,0 +1,21 @@
1
+export def "hmac sha256" [--key: oneof<binary, string>]: oneof<string, binary> -> binary {
2
+ let message = $in | into binary
3
+ let key = $key | into binary
4
+
5
+ const block_size = 64
6
7
+ let key_len = ($key | length)
8
+ let key = match $key_len {
9
+ 64 => $key,
10
+ 65.. => ($key | hash sha256 --binary),
11
+ _ => {bytes build $key (1..($block_size - $key_len) | each {0x[00]} | bytes collect)}
12
+ }
13
14
+ let i_key = $key | bits xor ((1..$block_size) | each {0x[36]} | bytes collect)
15
+ let o_key = $key | bits xor ((1..$block_size) | each {0x[5c]} | bytes collect)
16
17
+ bytes build $i_key $message
18
+ | hash sha256 --binary
19
+ | bytes build $o_key $in
20
21
+}
0 commit comments