summaryrefslogtreecommitdiffstats
path: root/lib/sha1.go
diff options
context:
space:
mode:
authorsiddharth <s@ricketyspace.net>2021-10-12 21:23:13 -0400
committersiddharth <s@ricketyspace.net>2021-10-12 21:23:13 -0400
commit194f13eb22e2ec821f42592b98017a1d7df36a33 (patch)
tree682d7daf2675989e31e817b5d6657a659630f156 /lib/sha1.go
parentfb7bbef174eee2ad5b5fc5f1bcd13487ba0d0f2e (diff)
lib: add HmacSha1
Diffstat (limited to 'lib/sha1.go')
-rw-r--r--lib/sha1.go25
1 files changed, 25 insertions, 0 deletions
diff --git a/lib/sha1.go b/lib/sha1.go
index 891ba83..fb81b92 100644
--- a/lib/sha1.go
+++ b/lib/sha1.go
@@ -236,3 +236,28 @@ func (s *Sha1) MacVerify(secret, msg, mac []byte) bool {
}
return false
}
+
+// HMAC-SHA1.
+func HmacSha1(key, msg []byte) []byte {
+ // Initialize SHA-1 object.
+ sha1 := Sha1{}
+ sha1.Init([]uint32{})
+
+ // Modify key based on it's size.
+ if len(key) > 64 { // > blocksize (64 bytes)
+ sha1.Message(key)
+ key = sha1.Hash()
+ }
+ if len(key) < 64 { // < blocksize (64 bytes)
+ // Pad with zeroes up to 64 bytes.
+ key = append(key, make([]byte, 64-len(key))...)
+ }
+
+ // Outer padded key.
+ opk := FixedXORBytes(key, FillBytes(0x5c, 64))
+
+ // Inner padded key.
+ ipk := FixedXORBytes(key, FillBytes(0x36, 64))
+
+ return sha1.Mac(opk, sha1.Mac(ipk, msg))
+}