This repository has been archived on 2024-07-06. You can view files and clone it, but cannot push or open issues or pull requests.
go-playit/proto/hmac.go
Matheus Sampaio Queiroga c756fc28b0
Update to same code from rust agent
Signed-off-by: Matheus Sampaio Queiroga <srherobrine20@gmail.com>
2024-05-24 23:28:31 -03:00

30 lines
500 B
Go

package proto
import (
"crypto/hmac"
"crypto/sha256"
"hash"
)
type HmacSha256 struct {
mac hash.Hash
}
func NewHmacSha256(secret []byte) *HmacSha256 {
mac := hmac.New(sha256.New, secret)
return &HmacSha256{mac}
}
func (h *HmacSha256) Verify(data, sig []byte) bool {
expectedMAC := h.mac.Sum(nil)
h.mac.Reset()
h.mac.Write(data)
return hmac.Equal(expectedMAC, sig)
}
func (h *HmacSha256) Sign(data []byte) [32]byte {
h.mac.Reset()
h.mac.Write(data)
return [32]byte(h.mac.Sum(nil))
}