Encrypt encrypts "data" with the specified "key" (must be valid 32 char AES key). This method uses AES-256-GCM block cypher mode.
(data []byte, key string)
| 12 | // |
| 13 | // This method uses AES-256-GCM block cypher mode. |
| 14 | func Encrypt(data []byte, key string) (string, error) { |
| 15 | block, err := aes.NewCipher([]byte(key)) |
| 16 | if err != nil { |
| 17 | return "", err |
| 18 | } |
| 19 | |
| 20 | gcm, err := cipher.NewGCM(block) |
| 21 | if err != nil { |
| 22 | return "", err |
| 23 | } |
| 24 | |
| 25 | nonce := make([]byte, gcm.NonceSize()) |
| 26 | |
| 27 | // populates the nonce with a cryptographically secure random sequence |
| 28 | if _, err := io.ReadFull(crand.Reader, nonce); err != nil { |
| 29 | return "", err |
| 30 | } |
| 31 | |
| 32 | cipherByte := gcm.Seal(nonce, nonce, data, nil) |
| 33 | |
| 34 | result := base64.StdEncoding.EncodeToString(cipherByte) |
| 35 | |
| 36 | return result, nil |
| 37 | } |
| 38 | |
| 39 | // Decrypt decrypts encrypted text with key (must be valid 32 chars AES key). |
| 40 | // |
no outgoing calls
searching dependent graphs…