Files
go-i2p/lib/crypto/hmac.go

44 lines
645 B
Go
Raw Normal View History

2016-01-28 10:16:26 -05:00
package crypto
import (
2016-01-29 07:22:31 -05:00
"crypto/md5"
2016-01-28 10:16:26 -05:00
)
const IPAD = byte(0x36)
const OPAD = byte(0x5C)
type HMACKey [32]byte
type HMACDigest [16]byte
func (hk HMACKey) xor(p byte) (i []byte) {
2016-01-29 07:22:31 -05:00
i = make([]byte, 64)
for idx, b := range hk {
i[idx] = b ^ p
}
c := 32
for c > 0 {
c--
i[c+32] = p
}
return
2016-01-28 10:16:26 -05:00
}
//
2016-01-29 07:22:31 -05:00
// do i2p hmac
2016-01-28 10:16:26 -05:00
//
func I2PHMAC(data []byte, k HMACKey) (d HMACDigest) {
2016-01-29 07:22:31 -05:00
buff := make([]byte, 64+len(data))
ip := k.xor(IPAD)
copy(buff, ip)
copy(buff[64:], data)
h := md5.Sum(buff)
buff = make([]byte, 96)
copy(buff, k.xor(OPAD))
copy(buff[64:], h[:])
// go zeros slices so we do not have to zero
d = md5.Sum(buff)
return
2016-01-28 10:16:26 -05:00
}