diff options
| author | siddharth <s@ricketyspace.net> | 2021-10-09 11:46:08 -0400 | 
|---|---|---|
| committer | siddharth <s@ricketyspace.net> | 2021-10-09 11:46:08 -0400 | 
| commit | fafa79cc3198e980e60de0b23af7e7ad03e37aad (patch) | |
| tree | 7166a8edddfca10c0aea79e74934be219697db86 | |
| parent | 21d8ade047ff938df52b7a1d85c65fcc9147db81 (diff) | |
lib: refactor sha1 implementation
Add ability to set initial hash values for Sha1
| -rw-r--r-- | challenge/c28.go | 9 | ||||
| -rw-r--r-- | lib/sha1.go | 30 | 
2 files changed, 29 insertions, 10 deletions
| diff --git a/challenge/c28.go b/challenge/c28.go index 7a763d5..a54be26 100644 --- a/challenge/c28.go +++ b/challenge/c28.go @@ -35,11 +35,14 @@ I'm pushin' away, I'm pushin' away  Yeah I'm pushin' away, pushin' away`)  	sec := lib.StrToBytes("Milk Records") +	// Init SHA1 +	sha1 := lib.Sha1{} +  	// Generate SHA1 MAC. -	mac := lib.Sha1Mac(sec, msg) +	mac := sha1.Mac(sec, msg)  	// Verify. -	if lib.Sha1MacVerify(sec, msg, mac) != true { +	if sha1.MacVerify(sec, msg, mac) != true {  		fmt.Printf("Error: Sha1Mac verification failed!\n")  		return  	} @@ -48,7 +51,7 @@ Yeah I'm pushin' away, pushin' away`)  	msg[42] = byte(42)  	// Verify that SHA1 MAC fails -	if lib.Sha1MacVerify(sec, msg, mac) != false { +	if sha1.MacVerify(sec, msg, mac) != false {  		fmt.Printf("Error: Sha1Mac verification success!\n")  		return  	} diff --git a/lib/sha1.go b/lib/sha1.go index bfe87b1..34f5f37 100644 --- a/lib/sha1.go +++ b/lib/sha1.go @@ -6,8 +6,12 @@ package lib  // SHA-1 implementation.  // Reference https://csrc.nist.gov/publications/detail/fips/180/4/final +type Sha1 struct { +	hvs []uint32 +} +  // Initial hash value. -var sha1IHashValue []uint32 = []uint32{ +var sha1IHashValues []uint32 = []uint32{  	0x67452301,  	0xefcdab89,  	0x98badcfe, @@ -149,7 +153,19 @@ func sha1MessageSchedule(mb []uint32) []uint32 {  	return w  } -func Sha1(m []byte) []byte { +func (s *Sha1) Init(hvs []uint32) { +	// Set Initial Hash Values. +	h := make([]uint32, 5) +	if len(hvs) == 5 { +		copy(h, hvs) +		s.hvs = h +	} else { +		copy(h, sha1IHashValues) +		s.hvs = h +	} +} + +func (s *Sha1) Hash(m []byte) []byte {  	// Pad message.  	pm := sha1Pad(m) @@ -158,7 +174,7 @@ func Sha1(m []byte) []byte {  	// Initialize hash values.  	h := make([]uint32, 5) -	copy(h, sha1IHashValue) // Initial hash values. +	copy(h, s.hvs) // Initial hash values.  	// Process each message block.  	for _, mb := range mbs { @@ -208,12 +224,12 @@ func Sha1(m []byte) []byte {  	return d  } -func Sha1Mac(secret, msg []byte) []byte { -	return Sha1(append(secret, msg...)) +func (s *Sha1) Mac(secret, msg []byte) []byte { +	return s.Hash(append(secret, msg...))  } -func Sha1MacVerify(secret, msg, mac []byte) bool { -	if BytesEqual(Sha1(append(secret, msg...)), mac) { +func (s *Sha1) MacVerify(secret, msg, mac []byte) bool { +	if BytesEqual(s.Hash(append(secret, msg...)), mac) {  		return true  	}  	return false | 
