crypto.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package utils
  2. import (
  3. "crypto/aes"
  4. "crypto/cipher"
  5. "crypto/rand"
  6. "crypto/sha1"
  7. "encoding/base64"
  8. "errors"
  9. "fmt"
  10. "io"
  11. "github.com/flet-dev/flet/server/config"
  12. )
  13. // SHA1 returns SHA1 hash of the input string.
  14. func SHA1(value string) string {
  15. h := sha1.New()
  16. io.WriteString(h, value)
  17. return fmt.Sprintf("%x", h.Sum(nil))
  18. }
  19. func EncryptWithMasterKey(data []byte) ([]byte, error) {
  20. return EncryptWithKey(data, config.MasterSecretKey())
  21. }
  22. func EncryptWithKey(data []byte, secretKey string) ([]byte, error) {
  23. block, err := aes.NewCipher(GetCipherKey(secretKey))
  24. if err != nil {
  25. return nil, err
  26. }
  27. aesgcm, err := cipher.NewGCM(block)
  28. if err != nil {
  29. panic(err.Error())
  30. }
  31. nonce := make([]byte, aesgcm.NonceSize())
  32. if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
  33. return nil, err
  34. }
  35. return aesgcm.Seal(nonce, nonce, data, nil), nil
  36. }
  37. func DecryptWithMasterKey(data []byte) ([]byte, error) {
  38. return DecryptWithKey(data, config.MasterSecretKey())
  39. }
  40. func DecryptWithKey(cipherData []byte, secretKey string) ([]byte, error) {
  41. block, err := aes.NewCipher(GetCipherKey(secretKey))
  42. if err != nil {
  43. return nil, err
  44. }
  45. aesgcm, err := cipher.NewGCM(block)
  46. if err != nil {
  47. return nil, err
  48. }
  49. nonceSize := aesgcm.NonceSize()
  50. if len(cipherData) < nonceSize {
  51. return nil, errors.New("invalid cipher data")
  52. }
  53. nonce, ciphertext := cipherData[:nonceSize], cipherData[nonceSize:]
  54. plaintext, err := aesgcm.Open(nil, nonce, ciphertext, nil)
  55. if err != nil {
  56. return nil, err
  57. }
  58. return plaintext, nil
  59. }
  60. func GetCipherKey(secretKey string) []byte {
  61. key := make([]byte, 32)
  62. for i, b := range []byte(secretKey)[:32] {
  63. key[i] = b
  64. }
  65. return key
  66. }
  67. // source: https://github.com/gorilla/securecookie/blob/master/securecookie.go
  68. func GenerateRandomKey(length int) []byte {
  69. k := make([]byte, length)
  70. if _, err := io.ReadFull(rand.Reader, k); err != nil {
  71. return nil
  72. }
  73. return k
  74. }
  75. func EncodeBase64(value []byte) string {
  76. encoded := make([]byte, base64.URLEncoding.EncodedLen(len(value)))
  77. base64.URLEncoding.Encode(encoded, value)
  78. return string(encoded)
  79. }
  80. func DecodeBase64(value string) ([]byte, error) {
  81. decoded := make([]byte, base64.URLEncoding.DecodedLen(len(value)))
  82. b, err := base64.URLEncoding.Decode(decoded, []byte(value))
  83. if err != nil {
  84. return nil, errors.New("base64 decode failed")
  85. }
  86. return decoded[:b], nil
  87. }