Created
March 17, 2018 09:03
-
-
Save silbinarywolf/cf3fa0aaa67aaa40c9fd5426cd947456 to your computer and use it in GitHub Desktop.
Debugging HMAC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
hmaclib "crypto/hmac" | |
"crypto/md5" | |
"encoding/hex" | |
"fmt" | |
"hash" | |
) | |
func ComputeHmacCorrect(message string, secret string) string { | |
h := hmaclib.New(md5.New, []byte(secret)) | |
h.Write([]byte(message)) | |
sum := h.Sum(nil) | |
return hex.EncodeToString(sum[:]) | |
} | |
func ComputeHmacCustom(message string, key_str string) string { | |
key := []byte(key_str) | |
h := md5.New | |
hm := new(hmac) | |
// New | |
{ | |
hm.outer = h() | |
hm.inner = h() | |
hm.size = hm.inner.Size() | |
hm.blocksize = hm.inner.BlockSize() | |
hm.ipad = make([]byte, hm.blocksize) | |
hm.opad = make([]byte, hm.blocksize) | |
if len(key) > hm.blocksize { | |
// If key is too big, hash it. | |
hm.outer.Write(key) | |
key = hm.outer.Sum(nil) | |
} | |
copy(hm.ipad, key) | |
copy(hm.opad, key) | |
for i := range hm.ipad { | |
hm.ipad[i] ^= 0x36 | |
} | |
for i := range hm.opad { | |
hm.opad[i] ^= 0x5c | |
} | |
hm.inner.Write(hm.ipad) | |
} | |
// DEBUG: Inner MD5 | |
{ | |
hm.Write([]byte(message)) | |
return hex.EncodeToString(hm.inner.Sum(nil)) | |
} | |
// Write | |
{ | |
hm.Write([]byte(message)) | |
} | |
// Sum(in) | |
var sum []byte | |
{ | |
var in []byte = nil | |
h := hm | |
origLen := len(in) | |
in = h.inner.Sum(in) | |
h.outer.Reset() | |
h.outer.Write(h.opad) | |
h.outer.Write(in[origLen:]) | |
//panic(fmt.Sprintf("%v", in[origLen:])) | |
sum = h.outer.Sum(in[:origLen]) | |
} | |
return hex.EncodeToString(sum[:]) | |
} | |
func main() { | |
secretKey := "secret_key" | |
//secretKey := "ff772e31410ae7f31b93c2109f496006ff772e31410ae7f31b93c2109f496006" | |
fmt.Printf("--------------------------------------------------------\n") | |
fmt.Printf("Normal implementation \n") | |
fmt.Printf("--------------------------------------------------------\n") | |
fmt.Printf("String A - %s\n", ComputeHmacCorrect(string([]byte{'A'}), secretKey)) | |
fmt.Printf("String A with zero byte terminator - %s\n", ComputeHmacCorrect(string([]byte{'A', 0}), secretKey)) | |
fmt.Printf("Empty string - %s\n", ComputeHmacCorrect("", secretKey)) | |
fmt.Printf("Empty string with zero byte - %s\n", ComputeHmacCorrect(string([]byte{0}), secretKey)) | |
fmt.Printf("--------------------------------------------------------\n") | |
fmt.Printf("Custom implementation \n") | |
fmt.Printf("--------------------------------------------------------\n") | |
fmt.Printf("String A - %s\n", ComputeHmacCustom(string([]byte{'A'}), secretKey)) | |
fmt.Printf("String A with zero byte terminator - %s\n", ComputeHmacCustom(string([]byte{'A', 0}), secretKey)) | |
fmt.Printf("Empty string - %s\n", ComputeHmacCustom("", secretKey)) | |
fmt.Printf("Empty string with zero byte - %s\n", ComputeHmacCustom(string([]byte{0}), secretKey)) | |
panic(".") | |
} | |
// --- | |
type hmac struct { | |
size int | |
blocksize int | |
opad, ipad []byte | |
outer, inner hash.Hash | |
} | |
func (h *hmac) Sum(in []byte) []byte { | |
origLen := len(in) | |
in = h.inner.Sum(in) | |
//in = []byte(hex.EncodeToString(h.inner.Sum(in))) | |
//fmt.Printf("Inner MD5: %s\n", hex.EncodeToString(in)) | |
h.outer.Reset() | |
h.outer.Write(h.opad) | |
h.outer.Write(in[origLen:]) | |
return h.outer.Sum(in[:origLen]) | |
} | |
func (h *hmac) Write(p []byte) (n int, err error) { | |
return h.inner.Write(p) | |
} | |
func (h *hmac) Size() int { return h.size } | |
func (h *hmac) BlockSize() int { return h.blocksize } | |
func (h *hmac) Reset() { | |
h.inner.Reset() | |
h.inner.Write(h.ipad) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment