From 194f13eb22e2ec821f42592b98017a1d7df36a33 Mon Sep 17 00:00:00 2001 From: siddharth Date: Tue, 12 Oct 2021 21:23:13 -0400 Subject: lib: add HmacSha1 --- lib/sha1.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'lib/sha1.go') 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)) +} -- cgit v1.2.3