/* * @Author: LinkLeong link@icewhale.com * @Date: 2022-06-14 14:33:25 * @LastEditors: LinkLeong * @LastEditTime: 2022-06-14 14:33:49 * @Description: * @Website: https://www.casaos.io * Copyright (c) 2022 by icewhale, All Rights Reserved. */ package encryption import ( "crypto/aes" "crypto/cipher" "crypto/md5" "crypto/rand" "encoding/hex" ) var ( // We're using a 32 byte long secret key. // This is probably something you generate first // then put into and environment variable. // secretKey string = config.AppInfo.SecretKey secretKey string = "" ) func GetMD5ByStr(str string) string { h := md5.New() h.Write([]byte(str)) return hex.EncodeToString(h.Sum(nil)) } func Encrypt(plaintext string) string { aes, err := aes.NewCipher([]byte(secretKey)) if err != nil { panic(err) } gcm, err := cipher.NewGCM(aes) if err != nil { panic(err) } // We need a 12-byte nonce for GCM (modifiable if you use cipher.NewGCMWithNonceSize()) // A nonce should always be randomly generated for every encryption. nonce := make([]byte, gcm.NonceSize()) _, err = rand.Read(nonce) if err != nil { panic(err) } // ciphertext here is actually nonce+ciphertext // So that when we decrypt, just knowing the nonce size // is enough to separate it from the ciphertext. ciphertext := gcm.Seal(nonce, nonce, []byte(plaintext), nil) return string(ciphertext) } func Decrypt(ciphertext string) string { aes, err := aes.NewCipher([]byte(secretKey)) if err != nil { panic(err) } gcm, err := cipher.NewGCM(aes) if err != nil { panic(err) } // Since we know the ciphertext is actually nonce+ciphertext // And len(nonce) == NonceSize(). We can separate the two. nonceSize := gcm.NonceSize() nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:] plaintext, err := gcm.Open(nil, []byte(nonce), []byte(ciphertext), nil) if err != nil { panic(err) } return string(plaintext) }