util.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package util
  2. import (
  3. "bytes"
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "crypto/rand"
  7. "crypto/rsa"
  8. "crypto/x509"
  9. "encoding/binary"
  10. "encoding/json"
  11. "fmt"
  12. "io"
  13. )
  14. // Signer interface
  15. type Signer interface {
  16. GetPrivateKey() *rsa.PrivateKey
  17. }
  18. // Check error function
  19. func Check(err error, args ...interface{}) {
  20. if err != nil {
  21. if len(args) == 0 {
  22. panic(err)
  23. } else {
  24. format := args[0].(string)
  25. args[0] = err
  26. panic(fmt.Errorf(format, append(args[1:], err)...))
  27. }
  28. }
  29. }
  30. // Uint64ToBytes func
  31. func Uint64ToBytes(v uint64) []byte {
  32. bb := make([]byte, 8)
  33. binary.LittleEndian.PutUint64(bb, v)
  34. return bb
  35. }
  36. // UintToBytes func
  37. func UintToBytes(v uint) []byte {
  38. bb := make([]byte, 4)
  39. binary.LittleEndian.PutUint32(bb, uint32(v))
  40. return bb
  41. }
  42. // KeyRSA func
  43. func KeyRSA() *rsa.PrivateKey {
  44. var privateKey *rsa.PrivateKey
  45. var err error
  46. if privateKey, err = rsa.GenerateKey(rand.Reader, 2048); err != nil {
  47. panic(err)
  48. }
  49. return privateKey
  50. }
  51. // MarshalPublicKey func
  52. func MarshalPublicKey(key *rsa.PublicKey) []byte {
  53. if data, err := x509.MarshalPKIXPublicKey(key); err != nil {
  54. panic(err)
  55. } else {
  56. return data
  57. }
  58. }
  59. // UnmarshalPublicKey func
  60. func UnmarshalPublicKey(key []byte) *rsa.PublicKey {
  61. var kk interface{}
  62. var err error
  63. if kk, err = x509.ParsePKIXPublicKey(key); err != nil {
  64. panic(err)
  65. }
  66. return kk.(*rsa.PublicKey)
  67. }
  68. // MarshalPrivateKey func
  69. func MarshalPrivateKey(key *rsa.PrivateKey) []byte {
  70. return x509.MarshalPKCS1PrivateKey(key)
  71. }
  72. // UnmarshalPrivateKey func
  73. func UnmarshalPrivateKey(key []byte) *rsa.PrivateKey {
  74. var kk *rsa.PrivateKey
  75. var err error
  76. if kk, err = x509.ParsePKCS1PrivateKey(key); err != nil {
  77. panic(err)
  78. }
  79. return kk
  80. }
  81. // JSONMarshal func
  82. func JSONMarshal(w io.Writer, v interface{}) {
  83. if d, err := json.Marshal(v); err == nil {
  84. w.Write(d)
  85. } else {
  86. panic(err)
  87. }
  88. }
  89. // JSONUnmarshal func
  90. func JSONUnmarshal(b []byte, v interface{}) interface{} {
  91. if err := json.Unmarshal(b, v); err != nil {
  92. panic(err)
  93. }
  94. return v
  95. }
  96. // PrivateKey func
  97. func PrivateKey() *rsa.PrivateKey {
  98. var err error
  99. var privateKey *rsa.PrivateKey
  100. if privateKey, err = rsa.GenerateKey(rand.Reader, 2048); err != nil {
  101. panic(err)
  102. }
  103. return privateKey
  104. }
  105. // EncryptRSA func
  106. func EncryptRSA(key *rsa.PublicKey, data []byte) []byte {
  107. var err error
  108. klen := key.N.BitLen()/8 - 11
  109. if len(data) <= klen {
  110. var bb []byte
  111. if bb, err = rsa.EncryptPKCS1v15(rand.Reader, key, data); err != nil {
  112. panic(err)
  113. }
  114. return bb
  115. }
  116. var buf bytes.Buffer
  117. var bb []byte
  118. for i, w, r := 0, 0, len(data); r > 0; i, r = i+w, r-w {
  119. if r <= klen {
  120. if bb, err = rsa.EncryptPKCS1v15(rand.Reader, key, data[i:]); err != nil {
  121. panic(err)
  122. }
  123. buf.Write(bb)
  124. w = r
  125. } else {
  126. if bb, err = rsa.EncryptPKCS1v15(rand.Reader, key, data[i:i+klen]); err != nil {
  127. panic(err)
  128. }
  129. buf.Write(bb)
  130. w = klen
  131. }
  132. }
  133. return buf.Bytes()
  134. }
  135. // DecryptRSA func
  136. func DecryptRSA(key *rsa.PrivateKey, data []byte) []byte {
  137. var err error
  138. klen := key.N.BitLen() / 8
  139. if len(data) <= klen {
  140. var bb []byte
  141. if bb, err = rsa.DecryptPKCS1v15(rand.Reader, key, data); err != nil {
  142. panic(err)
  143. }
  144. return bb
  145. }
  146. var buf bytes.Buffer
  147. var bb []byte
  148. for i, w, r := 0, 0, len(data); r > 0; i, r = i+w, r-w {
  149. if r <= klen {
  150. if bb, err = rsa.DecryptPKCS1v15(rand.Reader, key, data[i:]); err != nil {
  151. panic(err)
  152. }
  153. buf.Write(bb)
  154. w = r
  155. } else {
  156. if bb, err = rsa.DecryptPKCS1v15(rand.Reader, key, data[i:i+klen]); err != nil {
  157. panic(err)
  158. }
  159. buf.Write(bb)
  160. w = klen
  161. }
  162. }
  163. return buf.Bytes()
  164. }
  165. // KeyAES func
  166. func KeyAES() []byte {
  167. var err error
  168. var key = make([]byte, 32)
  169. if _, err = rand.Read(key); err != nil {
  170. panic(fmt.Errorf("Error generate aes key\n\n%+v", err))
  171. }
  172. return key
  173. }
  174. // EncryptAES func
  175. func EncryptAES(key, data []byte) []byte {
  176. block, err := aes.NewCipher(key)
  177. if err != nil {
  178. panic(err)
  179. }
  180. ciphertext := make([]byte, aes.BlockSize+len(data))
  181. iv := ciphertext[:aes.BlockSize]
  182. if _, err := io.ReadFull(rand.Reader, iv); err != nil {
  183. panic(err)
  184. }
  185. stream := cipher.NewCFBEncrypter(block, iv)
  186. stream.XORKeyStream(ciphertext[aes.BlockSize:], data)
  187. return ciphertext
  188. }
  189. // DecryptAES func
  190. func DecryptAES(key, ciphertext []byte) []byte {
  191. block, err := aes.NewCipher(key)
  192. if err != nil {
  193. panic(fmt.Errorf("Error creating new block cipher\n%v\n", err))
  194. }
  195. if len(ciphertext) < aes.BlockSize {
  196. panic(fmt.Errorf("ciphertext too short"))
  197. }
  198. iv := ciphertext[:aes.BlockSize]
  199. ciphertext = ciphertext[aes.BlockSize:]
  200. stream := cipher.NewCFBDecrypter(block, iv)
  201. // XORKeyStream can work in-place if the two arguments are the same.
  202. stream.XORKeyStream(ciphertext, ciphertext)
  203. return ciphertext
  204. }