auth.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package auth
  2. import (
  3. "bytes"
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "encoding/hex"
  7. )
  8. var secret = "0a113ef6b61820daa5611c870ed8d5ee"
  9. func AesDecrypt(token string) string {
  10. tokenByte, err := hex.DecodeString(token)
  11. if err != nil {
  12. return ""
  13. }
  14. block, err := aes.NewCipher([]byte(secret))
  15. if err != nil {
  16. //fmt.Println("err is:", err)
  17. return ""
  18. }
  19. blockMode := NewECBDecrypter(block)
  20. origData := make([]byte, len(tokenByte))
  21. blockMode.CryptBlocks(origData, []byte(tokenByte))
  22. origData = PKCS5UnPadding(origData)
  23. return string(origData)
  24. }
  25. func AesEncrypt(src string) string {
  26. block, err := aes.NewCipher([]byte(secret))
  27. if err != nil {
  28. return ""
  29. }
  30. //src := fmt.Sprintf("%v|%v|%v", uid, time.Now().UnixNano()/1000000, areaId)
  31. ecb := NewECBEncrypter(block)
  32. content := []byte(src)
  33. content = PKCS5Padding(content, block.BlockSize())
  34. crypted := make([]byte, len(content))
  35. ecb.CryptBlocks(crypted, content)
  36. return hex.EncodeToString(crypted)
  37. }
  38. func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
  39. padding := blockSize - len(ciphertext)%blockSize
  40. padtext := bytes.Repeat([]byte{byte(padding)}, padding)
  41. return append(ciphertext, padtext...)
  42. }
  43. func PKCS5UnPadding(origData []byte) []byte {
  44. length := len(origData)
  45. unpadding := int(origData[length-1])
  46. return origData[:(length - unpadding)]
  47. }
  48. type ecb struct {
  49. b cipher.Block
  50. blockSize int
  51. }
  52. func newECB(b cipher.Block) *ecb {
  53. return &ecb{
  54. b: b,
  55. blockSize: b.BlockSize(),
  56. }
  57. }
  58. type ecbEncrypter ecb
  59. // NewECBEncrypter returns a BlockMode which encrypts in electronic code book
  60. // mode, using the given Block.
  61. func NewECBEncrypter(b cipher.Block) cipher.BlockMode {
  62. return (*ecbEncrypter)(newECB(b))
  63. }
  64. func (x *ecbEncrypter) BlockSize() int { return x.blockSize }
  65. func (x *ecbEncrypter) CryptBlocks(dst, src []byte) {
  66. if len(src)%x.blockSize != 0 {
  67. panic("crypto/cipher: input not full blocks")
  68. }
  69. if len(dst) < len(src) {
  70. panic("crypto/cipher: output smaller than input")
  71. }
  72. for len(src) > 0 {
  73. x.b.Encrypt(dst, src[:x.blockSize])
  74. src = src[x.blockSize:]
  75. dst = dst[x.blockSize:]
  76. }
  77. }
  78. type ecbDecrypter ecb
  79. // NewECBDecrypter returns a BlockMode which decrypts in electronic code book
  80. // mode, using the given Block.
  81. func NewECBDecrypter(b cipher.Block) cipher.BlockMode {
  82. return (*ecbDecrypter)(newECB(b))
  83. }
  84. func (x *ecbDecrypter) BlockSize() int { return x.blockSize }
  85. func (x *ecbDecrypter) CryptBlocks(dst, src []byte) {
  86. if len(src)%x.blockSize != 0 {
  87. panic("crypto/cipher: input not full blocks")
  88. }
  89. if len(dst) < len(src) {
  90. panic("crypto/cipher: output smaller than input")
  91. }
  92. for len(src) > 0 {
  93. x.b.Decrypt(dst, src[:x.blockSize])
  94. src = src[x.blockSize:]
  95. dst = dst[x.blockSize:]
  96. }
  97. }
  98. func CalSign() (string, error) {
  99. return "", nil
  100. }