encrypt_util.go 965 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package util
  2. import "errors"
  3. // GenerateEncryptKey Generate a random EncryptKey
  4. func GenerateEncryptKey() (encryptKey string, err error) {
  5. key, nonce, err := GenerateKeyAndNonce()
  6. if err != nil {
  7. return "", err
  8. }
  9. return key + nonce, nil
  10. }
  11. // EncryptByEncryptKey 加密
  12. func EncryptByEncryptKey(encryptKey string, orgStr string) (ecryptStr string, err error) {
  13. if len(encryptKey) != 88 {
  14. return "", errors.New("EncryptKey not corret")
  15. }
  16. key, nonce, err := ValidateKeyAndNonce(encryptKey[0:64], encryptKey[64:88])
  17. if err != nil {
  18. return "", err
  19. }
  20. return Encrypt(key, nonce, orgStr)
  21. }
  22. // DecryptByEncryptKey 解密
  23. func DecryptByEncryptKey(encryptKey string, encryptStr string) (decryptStr string, err error) {
  24. if len(encryptKey) != 88 {
  25. return "", errors.New("EncryptKey not corret")
  26. }
  27. key, nonce, err := ValidateKeyAndNonce(encryptKey[0:64], encryptKey[64:88])
  28. if err != nil {
  29. return "", err
  30. }
  31. return Decrypt(key, nonce, encryptStr)
  32. }