xxtea.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // 网上导入有问题所有直接把代码复制过来
  2. // https://github.com/xxtea/xxtea-go.git
  3. package otherutils
  4. import (
  5. "encoding/base64"
  6. "strings"
  7. )
  8. const delta = 0x9E3779B9
  9. func toBytes(v []uint32, includeLength bool) []byte {
  10. length := uint32(len(v))
  11. n := length << 2
  12. if includeLength {
  13. m := v[length-1]
  14. n -= 4
  15. if (m < n-3) || (m > n) {
  16. return nil
  17. }
  18. n = m
  19. }
  20. bytes := make([]byte, n)
  21. for i := uint32(0); i < n; i++ {
  22. bytes[i] = byte(v[i>>2] >> ((i & 3) << 3))
  23. }
  24. return bytes
  25. }
  26. func toUint32s(bytes []byte, includeLength bool) (v []uint32) {
  27. length := uint32(len(bytes))
  28. n := length >> 2
  29. if length&3 != 0 {
  30. n++
  31. }
  32. if includeLength {
  33. v = make([]uint32, n+1)
  34. v[n] = length
  35. } else {
  36. v = make([]uint32, n)
  37. }
  38. for i := uint32(0); i < length; i++ {
  39. v[i>>2] |= uint32(bytes[i]) << ((i & 3) << 3)
  40. }
  41. return v
  42. }
  43. func mx(sum uint32, y uint32, z uint32, p uint32, e uint32, k []uint32) uint32 {
  44. return ((z>>5 ^ y<<2) + (y>>3 ^ z<<4)) ^ ((sum ^ y) + (k[p&3^e] ^ z))
  45. }
  46. func fixk(k []uint32) []uint32 {
  47. if len(k) < 4 {
  48. key := make([]uint32, 4)
  49. copy(key, k)
  50. return key
  51. }
  52. return k
  53. }
  54. func encrypt(v []uint32, k []uint32) []uint32 {
  55. length := uint32(len(v))
  56. n := length - 1
  57. k = fixk(k)
  58. var y, z, sum, e, p, q uint32
  59. z = v[n]
  60. sum = 0
  61. for q = 6 + 52/length; q > 0; q-- {
  62. sum += delta
  63. e = sum >> 2 & 3
  64. for p = 0; p < n; p++ {
  65. y = v[p+1]
  66. v[p] += mx(sum, y, z, p, e, k)
  67. z = v[p]
  68. }
  69. y = v[0]
  70. v[n] += mx(sum, y, z, p, e, k)
  71. z = v[n]
  72. }
  73. return v
  74. }
  75. func decrypt(v []uint32, k []uint32) []uint32 {
  76. length := uint32(len(v))
  77. n := length - 1
  78. k = fixk(k)
  79. var y, z, sum, e, p, q uint32
  80. y = v[0]
  81. q = 6 + 52/length
  82. for sum = q * delta; sum != 0; sum -= delta {
  83. e = sum >> 2 & 3
  84. for p = n; p > 0; p-- {
  85. z = v[p-1]
  86. v[p] -= mx(sum, y, z, p, e, k)
  87. y = v[p]
  88. }
  89. z = v[n]
  90. v[0] -= mx(sum, y, z, p, e, k)
  91. y = v[0]
  92. }
  93. return v
  94. }
  95. // Encrypt the data with key.
  96. // data is the bytes to be encrypted.
  97. // key is the encrypt key. It is the same as the decrypt key.
  98. func Encrypt(data []byte, key []byte) []byte {
  99. if data == nil || len(data) == 0 {
  100. return data
  101. }
  102. return toBytes(encrypt(toUint32s(data, true), toUint32s(key, false)), false)
  103. }
  104. // Decrypt the data with key.
  105. // data is the bytes to be decrypted.
  106. // key is the decrypted key. It is the same as the encrypt key.
  107. func Decrypt(data []byte, key []byte) []byte {
  108. if data == nil || len(data) == 0 {
  109. return data
  110. }
  111. return toBytes(decrypt(toUint32s(data, false), toUint32s(key, false)), true)
  112. }
  113. // Encrypt the data with key.
  114. // data is the string to be encrypted.
  115. // key is the string of encrypt key.
  116. func EncryptString(str, key string) string {
  117. s := []byte(str)
  118. k := []byte(key)
  119. b64 := base64.StdEncoding
  120. return b64.EncodeToString(Encrypt(s, k))
  121. }
  122. // Decrypt the data with key.
  123. // data is the string to be decrypted.
  124. // key is the decrypted key. It is the same as the encrypt key.
  125. func DecryptString(str, key string) (string, error) {
  126. k := []byte(key)
  127. b64 := base64.StdEncoding
  128. decodeStr, err := b64.DecodeString(str)
  129. if err != nil {
  130. return "", err
  131. }
  132. result := Decrypt([]byte(decodeStr), k)
  133. return string(result), nil
  134. }
  135. // Encrypt the string with key and convert the string to URL format
  136. func EncryptStdToURLString(str, key string) string {
  137. return encryptBase64ToUrlFormat(EncryptString(str, key))
  138. }
  139. // Decrypt the URL string with key and convert the URL string to the origin string
  140. func DecryptURLToStdString(str, key string) (string, error) {
  141. return DecryptString(decryptBase64ToStdFormat(str), key)
  142. }
  143. // Replace std character to URL character in base64 string
  144. func encryptBase64ToUrlFormat(str string) string {
  145. str = strings.Replace(str, "+", "-", -1)
  146. str = strings.Replace(str, "/", "_", -1)
  147. str = strings.Replace(str, "=", "~", -1)
  148. return str
  149. }
  150. // Replace URL character to origin character in base64 string
  151. func decryptBase64ToStdFormat(str string) string {
  152. str = strings.Replace(str, "-", "+", -1)
  153. str = strings.Replace(str, "_", "/", -1)
  154. str = strings.Replace(str, "~", "=", -1)
  155. return str
  156. }