nandi/freeqsay-nimpublic Fork 0
main
Commits
Clone
git clone https://git.rickub.com/nandi/freeqsay-nim.git
git clone ssh://git@rickub.com/nandi/freeqsay-nim.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

sha256.nim · 123 lines · 4.3 KBNim Blame HistoryRaw
freeqsay: Nim port of the Clojure implementation fa53aa2 nandi 6h ago1## Pure-Nim SHA-256, HMAC-SHA256 and HKDF-SHA256.
2##
3## No dependencies: the avatar seed has to be derivable anywhere the port runs,
4## including builds with no OpenSSL to bind against.
5
6import std/strutils
7
8type Sha256Digest* = array[32, byte]
9
10const K: array[64, uint32] = [
11 0x428a2f98'u32, 0x71374491'u32, 0xb5c0fbcf'u32, 0xe9b5dba5'u32,
12 0x3956c25b'u32, 0x59f111f1'u32, 0x923f82a4'u32, 0xab1c5ed5'u32,
13 0xd807aa98'u32, 0x12835b01'u32, 0x243185be'u32, 0x550c7dc3'u32,
14 0x72be5d74'u32, 0x80deb1fe'u32, 0x9bdc06a7'u32, 0xc19bf174'u32,
15 0xe49b69c1'u32, 0xefbe4786'u32, 0x0fc19dc6'u32, 0x240ca1cc'u32,
16 0x2de92c6f'u32, 0x4a7484aa'u32, 0x5cb0a9dc'u32, 0x76f988da'u32,
17 0x983e5152'u32, 0xa831c66d'u32, 0xb00327c8'u32, 0xbf597fc7'u32,
18 0xc6e00bf3'u32, 0xd5a79147'u32, 0x06ca6351'u32, 0x14292967'u32,
19 0x27b70a85'u32, 0x2e1b2138'u32, 0x4d2c6dfc'u32, 0x53380d13'u32,
20 0x650a7354'u32, 0x766a0abb'u32, 0x81c2c92e'u32, 0x92722c85'u32,
21 0xa2bfe8a1'u32, 0xa81a664b'u32, 0xc24b8b70'u32, 0xc76c51a3'u32,
22 0xd192e819'u32, 0xd6990624'u32, 0xf40e3585'u32, 0x106aa070'u32,
23 0x19a4c116'u32, 0x1e376c08'u32, 0x2748774c'u32, 0x34b0bcb5'u32,
24 0x391c0cb3'u32, 0x4ed8aa4a'u32, 0x5b9cca4f'u32, 0x682e6ff3'u32,
25 0x748f82ee'u32, 0x78a5636f'u32, 0x84c87814'u32, 0x8cc70208'u32,
26 0x90befffa'u32, 0xa4506ceb'u32, 0xbef9a3f7'u32, 0xc67178f2'u32]
27
28const H0: array[8, uint32] = [
29 0x6a09e667'u32, 0xbb67ae85'u32, 0x3c6ef372'u32, 0xa54ff53a'u32,
30 0x510e527f'u32, 0x9b05688c'u32, 0x1f83d9ab'u32, 0x5be0cd19'u32]
31
32func rotr(x: uint32, n: int): uint32 {.inline.} =
33 (x shr n) or (x shl (32 - n))
34
35func toBytes*(s: string): seq[byte] =
36 ## UTF-8 bytes of a string.
37 result = newSeq[byte](s.len)
38 for i, c in s:
39 result[i] = byte(c)
40
41func digest*(input: openArray[byte]): Sha256Digest =
42 let
43 n = input.len
44 bitlen = uint64(n) * 8
45 blocks = (n + 9 + 63) div 64
46 var padded = newSeq[byte](blocks * 64)
47 for i, b in input:
48 padded[i] = b
49 padded[n] = 0x80
50 for i in 0 ..< 8:
51 padded[padded.len - 1 - i] = byte((bitlen shr (8 * i)) and 0xff)
52
53 var h = H0
54 var w: array[64, uint32]
55 for blk in 0 ..< blocks:
56 let off = blk * 64
57 for i in 0 ..< 16:
58 w[i] = (uint32(padded[off + i*4]) shl 24) or
59 (uint32(padded[off + i*4 + 1]) shl 16) or
60 (uint32(padded[off + i*4 + 2]) shl 8) or
61 uint32(padded[off + i*4 + 3])
62 for i in 16 ..< 64:
63 let
64 w15 = w[i - 15]
65 w2 = w[i - 2]
66 s0 = rotr(w15, 7) xor rotr(w15, 18) xor (w15 shr 3)
67 s1 = rotr(w2, 17) xor rotr(w2, 19) xor (w2 shr 10)
68 w[i] = w[i - 16] + s0 + w[i - 7] + s1
69
70 var (a, b, c, d, e, f, g, hh) = (h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7])
71 for i in 0 ..< 64:
72 let
73 s1 = rotr(e, 6) xor rotr(e, 11) xor rotr(e, 25)
74 ch = (e and f) xor ((not e) and g)
75 t1 = hh + s1 + ch + K[i] + w[i]
76 s0 = rotr(a, 2) xor rotr(a, 13) xor rotr(a, 22)
77 maj = (a and b) xor (a and c) xor (b and c)
78 t2 = s0 + maj
79 hh = g; g = f; f = e; e = d + t1
80 d = c; c = b; b = a; a = t1 + t2
81
82 for i, v in [a, b, c, d, e, f, g, hh]:
83 h[i] = h[i] + v
84
85 for i, v in h:
86 result[i*4] = byte((v shr 24) and 0xff)
87 result[i*4 + 1] = byte((v shr 16) and 0xff)
88 result[i*4 + 2] = byte((v shr 8) and 0xff)
89 result[i*4 + 3] = byte(v and 0xff)
90
91func digest*(s: string): Sha256Digest = digest(toBytes(s))
92
93func hmac*(key, msg: openArray[byte]): Sha256Digest =
94 ## HMAC-SHA256.
95 var k = @key
96 if k.len > 64:
97 k = @(digest(k))
98 var ipad, opad = newSeq[byte](64)
99 for i in 0 ..< 64:
100 let kb = if i < k.len: k[i] else: 0'u8
101 ipad[i] = kb xor 0x36
102 opad[i] = kb xor 0x5c
103 let inner = digest(ipad & @msg)
104 digest(opad & @inner)
105
106func hmac*(key, msg: string): Sha256Digest = hmac(toBytes(key), toBytes(msg))
107
108func hkdf*(ikm, salt, info: openArray[byte], length = 32): seq[byte] =
109 ## HKDF-SHA256 (extract + expand). One expand block, so `length` <= 32.
110 doAssert length <= 32, "hkdf: only one expand block is implemented"
111 let prk = hmac(salt, ikm)
112 let t1 = hmac(prk, @info & @[1'u8])
113 result = @(t1)
114 result.setLen(length)
115
116func hkdf*(ikm, salt, info: string, length = 32): seq[byte] =
117 hkdf(toBytes(ikm), toBytes(salt), toBytes(info), length)
118
119func toHex*(bs: openArray[byte]): string =
120 ## Lowercase hex.
121 result = newStringOfCap(bs.len * 2)
122 for b in bs:
123 result.add toHex(int(b), 2).toLowerAscii