client.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package main
  2. import (
  3. "crypto/hmac"
  4. "crypto/md5"
  5. "crypto/sha256"
  6. "encoding/base64"
  7. "flag"
  8. "fmt"
  9. "io"
  10. "log"
  11. "net/http"
  12. "os"
  13. "strconv"
  14. "strings"
  15. "time"
  16. "github.com/tal-tech/go-zero/core/codec"
  17. )
  18. const pubKey = `-----BEGIN PUBLIC KEY-----
  19. MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD7bq4FLG0ctccbEFEsUBuRxkjE
  20. eJ5U+0CAEjJk20V9/u2Fu76i1oKoShCs7GXtAFbDb5A/ImIXkPY62nAaxTGK4KVH
  21. miYbRgh5Fy6336KepLCtCmV/r0PKZeCyJH9uYLs7EuE1z9Hgm5UUjmpHDhJtkAwR
  22. my47YlhspwszKdRP+wIDAQAB
  23. -----END PUBLIC KEY-----`
  24. var (
  25. crypt = flag.Bool("crypt", false, "encrypt body or not")
  26. key = []byte("q4t7w!z%C*F-JaNdRgUjXn2r5u8x/A?D")
  27. )
  28. func fingerprint(key string) string {
  29. h := md5.New()
  30. io.WriteString(h, key)
  31. return base64.StdEncoding.EncodeToString(h.Sum(nil))
  32. }
  33. func hs256(key []byte, body string) string {
  34. h := hmac.New(sha256.New, key)
  35. io.WriteString(h, body)
  36. return base64.StdEncoding.EncodeToString(h.Sum(nil))
  37. }
  38. func main() {
  39. flag.Parse()
  40. var err error
  41. body := "hello world!"
  42. if *crypt {
  43. bodyBytes, err := codec.EcbEncrypt(key, []byte(body))
  44. if err != nil {
  45. log.Fatal(err)
  46. }
  47. body = base64.StdEncoding.EncodeToString(bodyBytes)
  48. }
  49. r, err := http.NewRequest(http.MethodPost, "http://localhost:3333/a/b?c=first&d=second", strings.NewReader(body))
  50. if err != nil {
  51. log.Fatal(err)
  52. }
  53. timestamp := time.Now().Unix()
  54. sha := sha256.New()
  55. sha.Write([]byte(body))
  56. bodySign := fmt.Sprintf("%x", sha.Sum(nil))
  57. contentOfSign := strings.Join([]string{
  58. strconv.FormatInt(timestamp, 10),
  59. http.MethodPost,
  60. r.URL.Path,
  61. r.URL.RawQuery,
  62. bodySign,
  63. }, "\n")
  64. sign := hs256(key, contentOfSign)
  65. var mode string
  66. if *crypt {
  67. mode = "1"
  68. } else {
  69. mode = "0"
  70. }
  71. content := strings.Join([]string{
  72. "version=v1",
  73. "type=" + mode,
  74. fmt.Sprintf("key=%s", base64.StdEncoding.EncodeToString(key)),
  75. "time=" + strconv.FormatInt(timestamp, 10),
  76. }, "; ")
  77. encrypter, err := codec.NewRsaEncrypter([]byte(pubKey))
  78. if err != nil {
  79. log.Fatal(err)
  80. }
  81. output, err := encrypter.Encrypt([]byte(content))
  82. if err != nil {
  83. log.Fatal(err)
  84. }
  85. encryptedContent := base64.StdEncoding.EncodeToString(output)
  86. r.Header.Set("X-Content-Security", strings.Join([]string{
  87. fmt.Sprintf("key=%s", fingerprint(pubKey)),
  88. "secret=" + encryptedContent,
  89. "signature=" + sign,
  90. }, "; "))
  91. client := &http.Client{}
  92. resp, err := client.Do(r)
  93. if err != nil {
  94. log.Fatal(err)
  95. }
  96. defer resp.Body.Close()
  97. fmt.Println(resp.Status)
  98. io.Copy(os.Stdout, resp.Body)
  99. }