gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package secret
  4. import (
  5. "crypto/aes"
  6. "crypto/cipher"
  7. "crypto/rand"
  8. "crypto/sha256"
  9. "encoding/base64"
  10. "encoding/hex"
  11. "errors"
  12. "fmt"
  13. "io"
  14. )
  15. // AesEncrypt encrypts text and given key with AES.
  16. // It is only internally used at the moment to use "SECRET_KEY" for some database values.
  17. func AesEncrypt(key, text []byte) ([]byte, error) {
  18. block, err := aes.NewCipher(key)
  19. if err != nil {
  20. return nil, fmt.Errorf("AesEncrypt invalid key: %v", err)
  21. }
  22. b := base64.StdEncoding.EncodeToString(text)
  23. ciphertext := make([]byte, aes.BlockSize+len(b))
  24. iv := ciphertext[:aes.BlockSize]
  25. if _, err = io.ReadFull(rand.Reader, iv); err != nil {
  26. return nil, fmt.Errorf("AesEncrypt unable to read IV: %w", err)
  27. }
  28. cfb := cipher.NewCFBEncrypter(block, iv) //nolint:staticcheck // need to migrate and refactor to a new approach
  29. cfb.XORKeyStream(ciphertext[aes.BlockSize:], []byte(b))
  30. return ciphertext, nil
  31. }
  32. // AesDecrypt decrypts text and given key with AES.
  33. // It is only internally used at the moment to use "SECRET_KEY" for some database values.
  34. func AesDecrypt(key, text []byte) ([]byte, error) {
  35. block, err := aes.NewCipher(key)
  36. if err != nil {
  37. return nil, err
  38. }
  39. if len(text) < aes.BlockSize {
  40. return nil, errors.New("AesDecrypt ciphertext too short")
  41. }
  42. iv := text[:aes.BlockSize]
  43. text = text[aes.BlockSize:]
  44. cfb := cipher.NewCFBDecrypter(block, iv) //nolint:staticcheck // need to migrate and refactor to a new approach
  45. cfb.XORKeyStream(text, text)
  46. data, err := base64.StdEncoding.DecodeString(string(text))
  47. if err != nil {
  48. return nil, fmt.Errorf("AesDecrypt invalid decrypted base64 string: %w", err)
  49. }
  50. return data, nil
  51. }
  52. // EncryptSecret encrypts a string with given key into a hex string
  53. func EncryptSecret(key, str string) (string, error) {
  54. keyHash := sha256.Sum256([]byte(key))
  55. plaintext := []byte(str)
  56. ciphertext, err := AesEncrypt(keyHash[:], plaintext)
  57. if err != nil {
  58. return "", fmt.Errorf("failed to encrypt by secret: %w", err)
  59. }
  60. return hex.EncodeToString(ciphertext), nil
  61. }
  62. // DecryptSecret decrypts a previously encrypted hex string
  63. func DecryptSecret(key, cipherHex string) (string, error) {
  64. keyHash := sha256.Sum256([]byte(key))
  65. ciphertext, err := hex.DecodeString(cipherHex)
  66. if err != nil {
  67. return "", fmt.Errorf("failed to decrypt by secret, invalid hex string: %w", err)
  68. }
  69. plaintext, err := AesDecrypt(keyHash[:], ciphertext)
  70. if err != nil {
  71. return "", fmt.Errorf("failed to decrypt by secret, the key (maybe SECRET_KEY?) might be incorrect: %w", err)
  72. }
  73. return string(plaintext), nil
  74. }