-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #157 from Plaenkler/add-secret-encryption
[ADD] Encryption of sensitive data
- Loading branch information
Showing
9 changed files
with
170 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package cipher | ||
|
||
import ( | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"crypto/rand" | ||
"encoding/base64" | ||
"fmt" | ||
"io" | ||
) | ||
|
||
func Encrypt(key string, plaintext string) (string, error) { | ||
encrypter, err := aes.NewCipher([]byte(key)) | ||
if err != nil { | ||
return "", fmt.Errorf("[cipher-Encrypt-1] encryption failed: %s", err) | ||
} | ||
gcm, err := cipher.NewGCM(encrypter) | ||
if err != nil { | ||
return "", fmt.Errorf("[cipher-Encrypt-2] encryption failed: %s", err) | ||
} | ||
nonce := make([]byte, gcm.NonceSize()) | ||
_, err = io.ReadFull(rand.Reader, nonce) | ||
if err != nil { | ||
return "", fmt.Errorf("[cipher-Encrypt-3] encryption failed: %s", err) | ||
} | ||
return base64.StdEncoding.EncodeToString(gcm.Seal(nonce, nonce, []byte(plaintext), nil)), nil | ||
} | ||
|
||
func Decrypt(key string, ciphertext string) ([]byte, error) { | ||
c, err := aes.NewCipher([]byte(key)) | ||
if err != nil { | ||
return nil, fmt.Errorf("[cipher-Decrypt-1] decryption failed: %s", err) | ||
} | ||
gcm, err := cipher.NewGCM(c) | ||
if err != nil { | ||
return nil, fmt.Errorf("[cipher-Decrypt-2] decryption failed: %s", err) | ||
} | ||
cipherBytes, err := base64.StdEncoding.DecodeString(ciphertext) | ||
if err != nil { | ||
return nil, fmt.Errorf("[cipher-Decrypt-3] decryption failed: %s", err) | ||
} | ||
nonceSize := gcm.NonceSize() | ||
if len(cipherBytes) < nonceSize { | ||
return nil, fmt.Errorf("[cipher-Decrypt-4] decryption failed: %s", err) | ||
} | ||
nonce, cipherBytes := cipherBytes[:nonceSize], cipherBytes[nonceSize:] | ||
plaintext, err := gcm.Open(nil, nonce, cipherBytes, nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("[cipher-Decrypt-5] decryption failed: %s", err) | ||
} | ||
return plaintext, nil | ||
} | ||
|
||
func GenerateRandomKey(length int) (string, error) { | ||
randomBytes := make([]byte, length) | ||
_, err := rand.Read(randomBytes) | ||
if err != nil { | ||
return "", fmt.Errorf("[cipher-GenerateRandomKey-1] generating random key failed: %s", err) | ||
} | ||
return base64.URLEncoding.EncodeToString(randomBytes), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.