tea.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package yu_tencent
  2. import (
  3. yu_bytes "gogs.qqck.cn/s/tools/bytes"
  4. yu_rand "gogs.qqck.cn/s/tools/rand"
  5. )
  6. type Tea struct {
  7. key [4]uint32
  8. }
  9. func NewTea() (t *Tea) {
  10. t = &Tea{}
  11. return
  12. }
  13. func (t *Tea) SetKey(key [16]byte) *Tea {
  14. t.key[3] = uint32(key[15]) | uint32(key[14])<<8 | uint32(key[13])<<16 | uint32(key[12])<<24
  15. t.key[2] = uint32(key[11]) | uint32(key[10])<<8 | uint32(key[9])<<16 | uint32(key[8])<<24
  16. t.key[1] = uint32(key[7]) | uint32(key[6])<<8 | uint32(key[5])<<16 | uint32(key[4])<<24
  17. t.key[0] = uint32(key[3]) | uint32(key[2])<<8 | uint32(key[1])<<16 | uint32(key[0])<<24
  18. return t
  19. }
  20. func (t *Tea) SetKeyB(key []byte) *Tea {
  21. var j_key [16]byte
  22. copy(j_key[:], key)
  23. return t.SetKey(j_key)
  24. }
  25. func (t *Tea) GetKey() (key [16]byte) {
  26. key[0] = byte(t.key[0] >> 24)
  27. key[1] = byte(t.key[0] >> 16)
  28. key[2] = byte(t.key[0] >> 8)
  29. key[3] = byte(t.key[0])
  30. key[4] = byte(t.key[1] >> 24)
  31. key[5] = byte(t.key[1] >> 16)
  32. key[6] = byte(t.key[1] >> 8)
  33. key[7] = byte(t.key[1])
  34. key[8] = byte(t.key[2] >> 24)
  35. key[9] = byte(t.key[2] >> 16)
  36. key[10] = byte(t.key[2] >> 8)
  37. key[11] = byte(t.key[2])
  38. key[12] = byte(t.key[3] >> 24)
  39. key[13] = byte(t.key[3] >> 16)
  40. key[14] = byte(t.key[3] >> 8)
  41. key[15] = byte(t.key[3])
  42. return
  43. }
  44. func (t *Tea) GetKeyB() []byte {
  45. j_key := t.GetKey()
  46. return j_key[:]
  47. }
  48. func (t *Tea) rand() byte {
  49. return byte(yu_rand.Uint32())
  50. }
  51. func (t *Tea) En(data []byte) []byte {
  52. j_fill := 0x0a - (len(data)+1)%8 // 计算头部填充字节数
  53. j_encode := make([]byte, j_fill+len(data)+7) // 计算输出的密文长度
  54. // 这里的操作把 j_fill 存到了 j_encode 的第一个字节里面
  55. // 0xF8 后面三位是空的,正好留给 j_fill 因为 j_fill 是 0 到 7 的值,表示文本开始的字节位置
  56. j_encode[0] = byte(j_fill-3) | (t.rand() & 0xf8) // 存储pad长度
  57. // 这里用随机产生的数填充 j_encode[1] 到 j_encode[j_fill] 之间的内容
  58. for j_i := 1; j_i < j_fill; j_i++ {
  59. j_encode[j_i] = t.rand()
  60. }
  61. copy(j_encode[j_fill:], data)
  62. var j_iv1, j_iv2, j_holder uint64
  63. for j_i := 0; j_i < len(j_encode); j_i += 8 {
  64. j_holder = (uint64(j_encode[j_i+7]) | uint64(j_encode[j_i+6])<<8 |
  65. uint64(j_encode[j_i+5])<<16 | uint64(j_encode[j_i+4])<<24 |
  66. uint64(j_encode[j_i+3])<<32 | uint64(j_encode[j_i+2])<<40 |
  67. uint64(j_encode[j_i+1])<<48 | uint64(j_encode[j_i])<<56) ^ j_iv1
  68. j_iv1 = t.encode(j_holder)
  69. j_iv1 ^= j_iv2
  70. j_iv2 = j_holder
  71. j_encode[j_i] = byte(j_iv1 >> 56)
  72. j_encode[j_i+1] = byte(j_iv1 >> 48)
  73. j_encode[j_i+2] = byte(j_iv1 >> 40)
  74. j_encode[j_i+3] = byte(j_iv1 >> 32)
  75. j_encode[j_i+4] = byte(j_iv1 >> 24)
  76. j_encode[j_i+5] = byte(j_iv1 >> 16)
  77. j_encode[j_i+6] = byte(j_iv1 >> 8)
  78. j_encode[j_i+7] = byte(j_iv1)
  79. }
  80. return j_encode
  81. }
  82. //go:nosplit
  83. func (t *Tea) encode(n uint64) uint64 {
  84. v0, v1 := uint32(n>>32), uint32(n)
  85. v0 += (v1 + 0x9e3779b9) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  86. v1 += (v0 + 0x9e3779b9) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  87. v0 += (v1 + 0x3c6ef372) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  88. v1 += (v0 + 0x3c6ef372) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  89. v0 += (v1 + 0xdaa66d2b) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  90. v1 += (v0 + 0xdaa66d2b) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  91. v0 += (v1 + 0x78dde6e4) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  92. v1 += (v0 + 0x78dde6e4) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  93. v0 += (v1 + 0x1715609d) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  94. v1 += (v0 + 0x1715609d) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  95. v0 += (v1 + 0xb54cda56) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  96. v1 += (v0 + 0xb54cda56) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  97. v0 += (v1 + 0x5384540f) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  98. v1 += (v0 + 0x5384540f) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  99. v0 += (v1 + 0xf1bbcdc8) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  100. v1 += (v0 + 0xf1bbcdc8) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  101. v0 += (v1 + 0x8ff34781) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  102. v1 += (v0 + 0x8ff34781) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  103. v0 += (v1 + 0x2e2ac13a) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  104. v1 += (v0 + 0x2e2ac13a) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  105. v0 += (v1 + 0xcc623af3) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  106. v1 += (v0 + 0xcc623af3) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  107. v0 += (v1 + 0x6a99b4ac) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  108. v1 += (v0 + 0x6a99b4ac) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  109. v0 += (v1 + 0x08d12e65) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  110. v1 += (v0 + 0x08d12e65) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  111. v0 += (v1 + 0xa708a81e) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  112. v1 += (v0 + 0xa708a81e) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  113. v0 += (v1 + 0x454021d7) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  114. v1 += (v0 + 0x454021d7) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  115. v0 += (v1 + 0xe3779b90) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  116. v1 += (v0 + 0xe3779b90) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  117. return uint64(v0)<<32 | uint64(v1)
  118. }
  119. func (t *Tea) De(data []byte) []byte {
  120. j_data_len := len(data)
  121. if j_data_len < 9 || j_data_len%8 != 0 {
  122. return nil
  123. }
  124. j_decode := make([]byte, j_data_len)
  125. var j_iv1, j_iv2, j_holder uint64
  126. for j_i := 0; j_i < j_data_len; j_i += 8 {
  127. j_iv1 = uint64(data[j_i+7]) | uint64(data[j_i+6])<<8 |
  128. uint64(data[j_i+5])<<16 | uint64(data[j_i+4])<<24 |
  129. uint64(data[j_i+3])<<32 | uint64(data[j_i+2])<<40 |
  130. uint64(data[j_i+1])<<48 | uint64(data[j_i])<<56
  131. j_iv2 = t.decode(j_iv2 ^ j_iv1)
  132. j_holder ^= j_iv2
  133. j_decode[j_i] = byte(j_holder >> 56)
  134. j_decode[j_i+1] = byte(j_holder >> 48)
  135. j_decode[j_i+2] = byte(j_holder >> 40)
  136. j_decode[j_i+3] = byte(j_holder >> 32)
  137. j_decode[j_i+4] = byte(j_holder >> 24)
  138. j_decode[j_i+5] = byte(j_holder >> 16)
  139. j_decode[j_i+6] = byte(j_holder >> 8)
  140. j_decode[j_i+7] = byte(j_holder)
  141. j_holder = j_iv1
  142. }
  143. j_iv2_bytes := make([]byte, 8)
  144. j_iv2_bytes[0] = byte(j_iv2 >> 56)
  145. j_iv2_bytes[1] = byte(j_iv2 >> 48)
  146. j_iv2_bytes[2] = byte(j_iv2 >> 40)
  147. j_iv2_bytes[3] = byte(j_iv2 >> 32)
  148. j_iv2_bytes[4] = byte(j_iv2 >> 24)
  149. j_iv2_bytes[5] = byte(j_iv2 >> 16)
  150. j_iv2_bytes[6] = byte(j_iv2 >> 8)
  151. j_iv2_bytes[7] = byte(j_iv2)
  152. if !yu_bytes.Equals(j_iv2_bytes[1:8], data[j_data_len-15:j_data_len-8]) {
  153. return nil
  154. }
  155. return j_decode[j_decode[0]&7+3 : j_data_len-7]
  156. }
  157. //go:nosplit
  158. func (t *Tea) decode(n uint64) uint64 {
  159. v0, v1 := uint32(n>>32), uint32(n)
  160. v1 -= (v0 + 0xe3779b90) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  161. v0 -= (v1 + 0xe3779b90) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  162. v1 -= (v0 + 0x454021d7) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  163. v0 -= (v1 + 0x454021d7) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  164. v1 -= (v0 + 0xa708a81e) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  165. v0 -= (v1 + 0xa708a81e) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  166. v1 -= (v0 + 0x08d12e65) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  167. v0 -= (v1 + 0x08d12e65) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  168. v1 -= (v0 + 0x6a99b4ac) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  169. v0 -= (v1 + 0x6a99b4ac) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  170. v1 -= (v0 + 0xcc623af3) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  171. v0 -= (v1 + 0xcc623af3) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  172. v1 -= (v0 + 0x2e2ac13a) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  173. v0 -= (v1 + 0x2e2ac13a) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  174. v1 -= (v0 + 0x8ff34781) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  175. v0 -= (v1 + 0x8ff34781) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  176. v1 -= (v0 + 0xf1bbcdc8) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  177. v0 -= (v1 + 0xf1bbcdc8) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  178. v1 -= (v0 + 0x5384540f) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  179. v0 -= (v1 + 0x5384540f) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  180. v1 -= (v0 + 0xb54cda56) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  181. v0 -= (v1 + 0xb54cda56) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  182. v1 -= (v0 + 0x1715609d) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  183. v0 -= (v1 + 0x1715609d) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  184. v1 -= (v0 + 0x78dde6e4) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  185. v0 -= (v1 + 0x78dde6e4) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  186. v1 -= (v0 + 0xdaa66d2b) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  187. v0 -= (v1 + 0xdaa66d2b) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  188. v1 -= (v0 + 0x3c6ef372) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  189. v0 -= (v1 + 0x3c6ef372) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  190. v1 -= (v0 + 0x9e3779b9) ^ (v0<<4 + t.key[2]) ^ (v0>>5 + t.key[3])
  191. v0 -= (v1 + 0x9e3779b9) ^ (v1<<4 + t.key[0]) ^ (v1>>5 + t.key[1])
  192. return uint64(v0)<<32 | uint64(v1)
  193. }