deriveKey derives a 32-byte AES key from a password using iterated HMAC-SHA256.
(password []byte)
| 103 | |
| 104 | // deriveKey derives a 32-byte AES key from a password using iterated HMAC-SHA256. |
| 105 | func 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. |
| 123 | func Encrypt(plaintext string, key []byte) (string, error) { |
no outgoing calls