mac-cpu.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "box-cost/db/repo"
  5. "crypto"
  6. "crypto/rand"
  7. "crypto/rsa"
  8. "crypto/sha256"
  9. "crypto/x509"
  10. "encoding/base64"
  11. "encoding/json"
  12. "encoding/pem"
  13. "errors"
  14. "fmt"
  15. "io/ioutil"
  16. "os"
  17. "time"
  18. "github.com/gin-gonic/gin"
  19. "go.mongodb.org/mongo-driver/bson/primitive"
  20. )
  21. func Mac(r *GinRouter) {
  22. r.POST("/get/code", GetCode)
  23. }
  24. type DeviceInfo struct {
  25. DeviceId string
  26. CreateTime time.Time
  27. AppName string
  28. AppVersion string
  29. }
  30. const SECRET_KEY = "DFDFEXVEG"
  31. func GetCode(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  32. form := model.RegCodeReq{}
  33. err := c.ShouldBindJSON(&form)
  34. if err != nil {
  35. fmt.Println(err)
  36. return nil, err
  37. }
  38. fmt.Println(form)
  39. return nil, nil
  40. }
  41. func CreatePrivateKey() error {
  42. privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
  43. if err != nil {
  44. fmt.Println(err)
  45. return err
  46. }
  47. // 将私钥编码为PEM格式
  48. privateKeyPEM := pem.EncodeToMemory(&pem.Block{
  49. Type: "RSA PRIVATE KEY",
  50. Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
  51. })
  52. // 将私钥保存到文件中
  53. err = ioutil.WriteFile("certs/private_key.pem", privateKeyPEM, 0600)
  54. if err != nil {
  55. fmt.Println(err)
  56. return err
  57. }
  58. return nil
  59. }
  60. func CreatePublicKey(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  61. // privateKeyFile, err := os.Open(fileName)
  62. privateKeyFile, err := os.Open("certs/private_key.pem")
  63. if err != nil {
  64. fmt.Println(err)
  65. return nil, err
  66. }
  67. defer privateKeyFile.Close()
  68. privateKeyPEM, err := ioutil.ReadAll(privateKeyFile)
  69. if err != nil {
  70. fmt.Println(err)
  71. return nil, err
  72. }
  73. privateKeyBlock, _ := pem.Decode(privateKeyPEM)
  74. privateKey, err := x509.ParsePKCS1PrivateKey(privateKeyBlock.Bytes)
  75. if err != nil {
  76. fmt.Println(err)
  77. return nil, err
  78. }
  79. // 根据私钥生成公钥
  80. publicKey1 := privateKey.PublicKey
  81. // 根据index的 不同生成不同的publickey
  82. index, err := repo.RepoCountDoc(apictx.CreateRepoCtx(), "reg-code", repo.Map{"status": 1})
  83. if err != nil {
  84. fmt.Println(err)
  85. return nil, err
  86. }
  87. publicKey := rsa.PublicKey{
  88. N: publicKey1.N,
  89. E: 65537 + int(index),
  90. }
  91. publicKeyPEM := pem.EncodeToMemory(&pem.Block{
  92. Type: "RSA PUBLIC KEY",
  93. Bytes: x509.MarshalPKCS1PublicKey(&publicKey),
  94. })
  95. // 将公钥保存到文件中
  96. err = ioutil.WriteFile("public_key.pem", publicKeyPEM, 0644)
  97. if err != nil {
  98. fmt.Println(err)
  99. return nil, err
  100. }
  101. return true, nil
  102. }
  103. // 生成激活码
  104. func CreateActiveCode(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  105. // 根据注册码生成激活码
  106. // 解析注册码
  107. type RegCodeReq struct {
  108. Cipher string
  109. }
  110. form := RegCodeReq{}
  111. err := c.ShouldBindJSON(&form)
  112. if err != nil {
  113. return nil, err
  114. }
  115. if len(form.Cipher) < 1 {
  116. return nil, errors.New("注册码错误")
  117. }
  118. privateFile, err := os.Open("private_key.pem")
  119. if err != nil {
  120. fmt.Println(err)
  121. return false, err
  122. }
  123. defer privateFile.Close()
  124. privateKeyPEM, err := ioutil.ReadAll(privateFile)
  125. if err != nil {
  126. fmt.Println(err)
  127. return false, errors.New("decode err")
  128. }
  129. privateKeyBlock, _ := pem.Decode(privateKeyPEM)
  130. if privateKeyBlock == nil {
  131. return false, errors.New("decode err")
  132. }
  133. privateKey, err := x509.ParsePKCS1PrivateKey(privateKeyBlock.Bytes)
  134. if err != nil {
  135. return false, err
  136. }
  137. decrypted, err := rsa.DecryptOAEP(
  138. sha256.New(),
  139. rand.Reader,
  140. privateKey,
  141. []byte(form.Cipher),
  142. []byte(""),
  143. )
  144. if err != nil {
  145. fmt.Println(err)
  146. return false, err
  147. }
  148. decoded, err := base64.StdEncoding.DecodeString(string(decrypted))
  149. if err != nil {
  150. return nil, err
  151. }
  152. regCodeReq := model.RegCodeReq{}
  153. err = json.Unmarshal(decoded, &regCodeReq)
  154. if err != nil {
  155. return nil, err
  156. }
  157. fmt.Println(regCodeReq)
  158. // 数据库操作
  159. _id, err := repo.RepoAddDoc(apictx.CreateRepoCtx(), "reg-code", &model.RegCode{
  160. UserId: apictx.User.Parent,
  161. AppId: regCodeReq.AppId,
  162. AppName: regCodeReq.AppName,
  163. AppVersion: regCodeReq.AppVersion,
  164. DeviceId: regCodeReq.DeviceId,
  165. // RegCode: form.Code,
  166. // ActiveCode
  167. IsPerm: regCodeReq.IsPerm,
  168. CreateTime: time.Now(),
  169. UpdateTIme: time.Now(),
  170. ExpireTime: regCodeReq.ExpireTime,
  171. Status: 1,
  172. })
  173. if err != nil {
  174. return nil, err
  175. }
  176. // 组装返回数据
  177. id, _ := primitive.ObjectIDFromHex(_id)
  178. // Calculate hash of ciphertext
  179. hash := sha256.Sum256([]byte(form.Cipher))
  180. // Generate signature
  181. signature, err := rsa.SignPKCS1v15(
  182. rand.Reader,
  183. privateKey,
  184. crypto.SHA256,
  185. hash[:],
  186. )
  187. if err != nil {
  188. fmt.Println(err)
  189. return nil, err
  190. }
  191. resp := &model.RegCodeResp{
  192. Id: id,
  193. AppId: regCodeReq.AppId,
  194. AppName: regCodeReq.AppName,
  195. AppVersion: regCodeReq.AppVersion,
  196. Signature: string(signature),
  197. CipherText: form.Cipher,
  198. }
  199. return resp, nil
  200. }