MCPcopy Create free account
hub / github.com/Driver-C/tryssh / deriveKey

Function deriveKey

pkg/utils/crypto.go:105–120  ·  view source on GitHub ↗

deriveKey derives a 32-byte AES key from a password using iterated HMAC-SHA256.

(password []byte)

Source from the content-addressed store, hash-verified

103
104// deriveKey derives a 32-byte AES key from a password using iterated HMAC-SHA256.
105func deriveKey(password []byte) ([]byte, error) {
106 if len(password) < 4 {
107 return nil, fmt.Errorf("master password must be at least 4 characters")
108 }
109 salt := []byte("tryssh-config-v1")
110 key := make([]byte, 32)
111 h := hmac.New(sha256.New, password)
112 h.Write(salt)
113 copy(key, h.Sum(nil))
114 for i := 0; i < kdfIter; i++ {
115 h = hmac.New(sha256.New, password)
116 h.Write(key)
117 key = h.Sum(key[:0])
118 }
119 return key, nil
120}
121
122// Encrypt encrypts plaintext and returns a prefixed base64 string.
123func Encrypt(plaintext string, key []byte) (string, error) {

Callers 8

GetMasterKeyFunction · 0.85
GetCachedMasterKeyFunction · 0.85
TestDeriveKey_TooShortFunction · 0.85
TestDeriveKey_ValidFunction · 0.85
TestDeriveKey_MinLengthFunction · 0.85

Calls

no outgoing calls

Tested by 6

TestDeriveKey_TooShortFunction · 0.68
TestDeriveKey_ValidFunction · 0.68
TestDeriveKey_MinLengthFunction · 0.68