kdf.go 435 B

1234567891011121314151617181920212223242526
  1. package aead
  2. import (
  3. "crypto/hmac"
  4. "crypto/sha256"
  5. "hash"
  6. )
  7. func KDF(key []byte, path ...string) []byte {
  8. hmacf := hmac.New(func() hash.Hash {
  9. return sha256.New()
  10. }, []byte("VMess AEAD KDF"))
  11. for _, v := range path {
  12. hmacf = hmac.New(func() hash.Hash {
  13. return hmacf
  14. }, []byte(v))
  15. }
  16. hmacf.Write(key)
  17. return hmacf.Sum(nil)
  18. }
  19. func KDF16(key []byte, path ...string) []byte {
  20. r := KDF(key, path...)
  21. return r[:16]
  22. }