You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
2.5 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "github.com/fatih/color"
  7. "github.com/gorilla/mux"
  8. )
  9. //TODO use rsa library instead own rsa functions
  10. func Index(w http.ResponseWriter, r *http.Request) {
  11. fmt.Fprintln(w, "clientApp")
  12. }
  13. func GetServer(w http.ResponseWriter, r *http.Request) {
  14. color.Green(config.Server)
  15. fmt.Fprintln(w, config.Server)
  16. }
  17. func GetIDs(w http.ResponseWriter, r *http.Request) {
  18. keys := IDs()
  19. jResp, err := json.Marshal(keys)
  20. check(err)
  21. fmt.Fprintln(w, string(jResp))
  22. }
  23. func GetID(w http.ResponseWriter, r *http.Request) {
  24. vars := mux.Vars(r)
  25. keyID := vars["keyid"]
  26. key := getKeyByKeyID(keyID)
  27. jResp, err := json.Marshal(key)
  28. check(err)
  29. fmt.Fprintln(w, string(jResp))
  30. }
  31. func GetNewID(w http.ResponseWriter, r *http.Request) {
  32. key := NewID()
  33. fmt.Println(key)
  34. jResp, err := json.Marshal(key)
  35. check(err)
  36. fmt.Fprintln(w, string(jResp))
  37. }
  38. func GetBlindAndSendToSign(w http.ResponseWriter, r *http.Request) {
  39. vars := mux.Vars(r)
  40. idKey := vars["keyid"]
  41. color.Green(idKey)
  42. keys := BlindAndSendToSign(idKey)
  43. jResp, err := json.Marshal(keys)
  44. check(err)
  45. fmt.Fprintln(w, string(jResp))
  46. }
  47. func GetVerify(w http.ResponseWriter, r *http.Request) {
  48. vars := mux.Vars(r)
  49. packPubK := vars["pubK"]
  50. color.Green(packPubK)
  51. //keys := Verify(packPubK)
  52. keys := "a"
  53. jResp, err := json.Marshal(keys)
  54. check(err)
  55. fmt.Fprintln(w, string(jResp))
  56. }
  57. func GetDelete(w http.ResponseWriter, r *http.Request) {
  58. vars := mux.Vars(r)
  59. keyID := vars["keyid"]
  60. keys := Delete(keyID)
  61. jResp, err := json.Marshal(keys)
  62. check(err)
  63. fmt.Fprintln(w, string(jResp))
  64. }
  65. type EncryptData struct {
  66. M string `json:"m"`
  67. C []byte `json:"c"`
  68. }
  69. func PostEncrypt(w http.ResponseWriter, r *http.Request) {
  70. vars := mux.Vars(r)
  71. keyID := vars["keyid"]
  72. //get ciphertext from POST json
  73. decoder := json.NewDecoder(r.Body)
  74. var encryptData EncryptData
  75. err := decoder.Decode(&encryptData)
  76. if err != nil {
  77. panic(err)
  78. }
  79. defer r.Body.Close()
  80. encryptData = Encrypt(keyID, encryptData)
  81. jResp, err := json.Marshal(encryptData)
  82. check(err)
  83. fmt.Fprintln(w, string(jResp))
  84. }
  85. func PostDecrypt(w http.ResponseWriter, r *http.Request) {
  86. vars := mux.Vars(r)
  87. keyID := vars["keyid"]
  88. //get ciphertext from POST json
  89. decoder := json.NewDecoder(r.Body)
  90. var encryptData EncryptData
  91. err := decoder.Decode(&encryptData)
  92. if err != nil {
  93. panic(err)
  94. }
  95. defer r.Body.Close()
  96. encryptData = Decrypt(keyID, encryptData)
  97. jResp, err := json.Marshal(encryptData)
  98. check(err)
  99. fmt.Fprintln(w, string(jResp))
  100. }