aes.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package gauth
  2. import (
  3. "crypto/aes"
  4. "fmt"
  5. "strings"
  6. b64 "encoding/base64"
  7. "github.com/happierall/l"
  8. "github.com/metarare/metarare_api/helpers"
  9. )
  10. func prepareForAES() (string, error) {
  11. vv := helpers.LoadEnvs()
  12. v := vv.GetString("jwt.aes_secret")
  13. if strings.Compare(v, "") != 0 {
  14. return v, nil
  15. } else {
  16. l.Error("There is no JWTKey ")
  17. return "", fmt.Errorf("Threr is no AES SECRET!!!")
  18. }
  19. }
  20. func encrpyt(plainText string) ([]byte, error) {
  21. k, err := prepareForAES()
  22. if err != nil {
  23. l.Error(err.Error())
  24. return nil, err
  25. }
  26. if len(k) < len(plainText) {
  27. err := fmt.Errorf("Plain text is too long...(Max size %d)", len(k))
  28. l.Errorf(err.Error())
  29. return nil, err
  30. }
  31. if len(plainText) > 255 {
  32. err := fmt.Errorf("Plain data is too long MAX SIZE is 255")
  33. l.Errorf(err.Error())
  34. return nil, err
  35. }
  36. tempData := []byte(k[:aes.BlockSize])
  37. block, err := aes.NewCipher(tempData)
  38. if err != nil {
  39. l.Error(err)
  40. return nil, err
  41. }
  42. devidedText := make([]string, len(plainText)/aes.BlockSize+1)
  43. lastBlockSize := len(plainText) % aes.BlockSize
  44. for i := 0; i < len(devidedText)-1; i++ {
  45. devidedText[i] = plainText[i*aes.BlockSize : (i+1)*aes.BlockSize]
  46. }
  47. padding := make([]byte, aes.BlockSize-lastBlockSize)
  48. devidedText[len(devidedText)-1] = fmt.Sprintf("%s%s", plainText[aes.BlockSize*(len(devidedText)-1):len(plainText)], string(padding))
  49. // NOTE It work good even if doesn't do initializing why?
  50. //devidedText[len(devidedText)-1 ] = append( devidedText[len(devidedText)-1 ], padding)
  51. prefix := make([]byte, 1) // 마지막 블럭의 사이즈
  52. prefix[0] = byte(lastBlockSize) & 0xff
  53. //encData := string(prefix)
  54. encData := prefix
  55. //store := make([]byte, aes.BlockSize*len(devidedText)+1)
  56. store := make([]byte, aes.BlockSize)
  57. for i := range devidedText {
  58. block.Encrypt(store, []byte(devidedText[i]))
  59. //encData = encData + string(store)
  60. encData = append(encData, store...)
  61. // l.Log("block data : (%s) , totalString (%s)", store, encData)
  62. }
  63. //return fmt.Sprintf("%s", encData), nil
  64. return encData, nil
  65. }
  66. func E(text string) []byte {
  67. if v, err := encrpyt(text); err != nil {
  68. l.Error("invalid value")
  69. return nil
  70. } else {
  71. return v
  72. }
  73. }
  74. func Pack(raw string) string {
  75. encData := E(raw)
  76. b64EncData := b64.URLEncoding.EncodeToString(encData)
  77. return b64EncData
  78. }
  79. func decrpyt(encData string) ([]byte, error) {
  80. k, err := prepareForAES()
  81. if err != nil {
  82. return nil, err
  83. }
  84. if len(k) < len(encData) {
  85. err := fmt.Errorf("Plain text is too long...(Max size %d)", len(k))
  86. l.Errorf(err.Error())
  87. return nil, err
  88. }
  89. if len(encData) > 255 {
  90. err := fmt.Errorf("Plain data is too long MAX SIZE is 255")
  91. l.Errorf(err.Error())
  92. return nil, err
  93. }
  94. if len(encData)%aes.BlockSize != 1 {
  95. // encData size have to fit [len(encData) =16 * n + 1]
  96. err := fmt.Errorf("encData size have to fit [len(encData) =16 * n + 1]")
  97. l.Errorf(err.Error())
  98. return nil, err
  99. }
  100. block, err := aes.NewCipher([]byte(k[:aes.BlockSize]))
  101. if err != nil {
  102. l.Error(err)
  103. return nil, err
  104. }
  105. lastBlockSize := int([]byte(encData[:1])[0] & 0xff)
  106. leftEncData := []byte(encData[1:len(encData)])
  107. /// new
  108. devidedTextLen := len(leftEncData) / aes.BlockSize
  109. if len(leftEncData)%aes.BlockSize != 0 {
  110. devidedTextLen++
  111. }
  112. devidedText := make([][]byte, devidedTextLen)
  113. for i := range devidedText {
  114. devidedText[i] = leftEncData[aes.BlockSize*i : aes.BlockSize*(i+1)]
  115. }
  116. plainBinary := make([]byte, 0)
  117. store := make([]byte, aes.BlockSize)
  118. for i := 0; i < devidedTextLen; i++ {
  119. block.Decrypt(store, []byte(devidedText[i]))
  120. plainBinary = append(plainBinary, store...)
  121. }
  122. return plainBinary[:len(leftEncData)-(aes.BlockSize-lastBlockSize)], nil
  123. }
  124. func D(encData string) []byte {
  125. if v, err := decrpyt(encData); err != nil {
  126. l.Error("invalid value")
  127. return nil
  128. } else {
  129. return v
  130. }
  131. }
  132. func UnPack(b64EncData string) string {
  133. decoded64Data, _ := b64.URLEncoding.DecodeString(b64EncData)
  134. decData := D(string(decoded64Data))
  135. return string(decData)
  136. }