Skip to content

Commit 6f9d78b

Browse files
authored
Implement HMAC SHA256 function in nushell
1 parent 5401eae commit 6f9d78b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

modules/crypto/hmac.nu

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
| hash sha256 --binary
21+
}

0 commit comments

Comments
 (0)