mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-11-14 00:07:09 -05:00
35 lines
661 B
Go
35 lines
661 B
Go
package aead
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/sha256"
|
|
"hash"
|
|
)
|
|
|
|
func KDF(key []byte, path ...string) []byte {
|
|
hmacCreator := &hMacCreator{value: []byte(KDFSaltConstVMessAEADKDF)}
|
|
for _, v := range path {
|
|
hmacCreator = &hMacCreator{value: []byte(v), parent: hmacCreator}
|
|
}
|
|
hmacf := hmacCreator.Create()
|
|
hmacf.Write(key)
|
|
return hmacf.Sum(nil)
|
|
}
|
|
|
|
type hMacCreator struct {
|
|
parent *hMacCreator
|
|
value []byte
|
|
}
|
|
|
|
func (h *hMacCreator) Create() hash.Hash {
|
|
if h.parent == nil {
|
|
return hmac.New(sha256.New, h.value)
|
|
}
|
|
return hmac.New(h.parent.Create, h.value)
|
|
}
|
|
|
|
func KDF16(key []byte, path ...string) []byte {
|
|
r := KDF(key, path...)
|
|
return r[:16]
|
|
}
|