kdf.go 521 B

123456789101112131415161718192021222324252627282930313233
  1. package aead
  2. import (
  3. "crypto/hmac"
  4. "crypto/sha256"
  5. "hash"
  6. )
  7. type hash2 struct {
  8. hash.Hash
  9. }
  10. func KDF(key []byte, path ...string) []byte {
  11. hmacf := hmac.New(sha256.New, []byte(KDFSaltConstVMessAEADKDF))
  12. for _, v := range path {
  13. first := true
  14. hmacf = hmac.New(func() hash.Hash {
  15. if first {
  16. first = false
  17. return hash2{hmacf}
  18. }
  19. return hmacf
  20. }, []byte(v))
  21. }
  22. hmacf.Write(key)
  23. return hmacf.Sum(nil)
  24. }
  25. func KDF16(key []byte, path ...string) []byte {
  26. r := KDF(key, path...)
  27. return r[:16]
  28. }