|
@@ -1,6 +1,8 @@
|
|
|
package common
|
|
|
|
|
|
import (
|
|
|
+ "crypto/aes"
|
|
|
+ "crypto/cipher"
|
|
|
"crypto/md5"
|
|
|
"encoding/hex"
|
|
|
"fmt"
|
|
@@ -64,3 +66,51 @@ func MD5(v []byte) string {
|
|
|
re := h.Sum(nil)
|
|
|
return hex.EncodeToString(re)
|
|
|
}
|
|
|
+
|
|
|
+var dbPwKey = []byte("X3O6wVF&6*&lSVk0*504V~q7>\"k]6S'*") // 32 bytes for AES-256
|
|
|
+var dbPwNonceHex = "1962a6f6f9999447632c8a34"
|
|
|
+
|
|
|
+func EncryptGCM(key []byte, nonce []byte, plaintext []byte) ([]byte, error) {
|
|
|
+ block, err := aes.NewCipher(key)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ gcm, err := cipher.NewGCM(block)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ ciphertext := gcm.Seal(nil, nonce, plaintext, nonce)
|
|
|
+
|
|
|
+ return ciphertext, nil
|
|
|
+}
|
|
|
+
|
|
|
+func DecryptGCM(key []byte, nonce []byte, ciphertext []byte) ([]byte, error) {
|
|
|
+ block, err := aes.NewCipher(key)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ gcm, err := cipher.NewGCM(block)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ plaintext, err := gcm.Open(nil, nonce, ciphertext, nonce)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ return plaintext, nil
|
|
|
+}
|
|
|
+
|
|
|
+func DBPwdEncrypt(ciphertext []byte) ([]byte, error) {
|
|
|
+ nonce, _ := hex.DecodeString(dbPwNonceHex)
|
|
|
+ return EncryptGCM(dbPwKey, nonce, ciphertext)
|
|
|
+}
|
|
|
+
|
|
|
+func DBPwdDecrypt(ciphertext []byte) ([]byte, error) {
|
|
|
+ nonce, _ := hex.DecodeString(dbPwNonceHex)
|
|
|
+ return DecryptGCM(dbPwKey, nonce, ciphertext)
|
|
|
+}
|