liscense-utils_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package comm
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "math/rand"
  6. "testing"
  7. "time"
  8. )
  9. // 长度为62
  10. var bytes []byte = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890")
  11. func init() {
  12. // 保证每次生成的随机数不一样
  13. rand.Seed(time.Now().UnixNano())
  14. }
  15. // 方法一
  16. func RandStr1(n int) string {
  17. result := make([]byte, n)
  18. for i := 0; i < n; i++ {
  19. result[i] = bytes[rand.Int31()%62]
  20. }
  21. return string(result)
  22. }
  23. // 方法二
  24. func RandStr2(n int) string {
  25. result := make([]byte, n/2)
  26. rand.Read(result)
  27. return hex.EncodeToString(result)
  28. }
  29. func TestRandom32(t *testing.T) {
  30. fmt.Println(RandStr1(32))
  31. }
  32. func TestAppKey(t *testing.T) {
  33. appId := "screen"
  34. appKey := "zjIWxbpOxE9lQaRuiO1lLmed7UqWYVjo"
  35. secrate := GenSecretKey(appId, appKey)
  36. t.Error("xxxxx")
  37. fmt.Printf("appId %s key: %s => %s \n", appId, appKey, secrate)
  38. }
  39. func TestEncode(t *testing.T) {
  40. secrate := "rxk1ly9+IPbBMJkcyUS53wYrh1owMDAwMDAwMDAwMDA="
  41. c, e := Encode(secrate, []byte("hello world"))
  42. if e != nil {
  43. t.Error(e)
  44. }
  45. fmt.Printf("encoded => %s \n", c)
  46. ret, e := Decode(secrate, c)
  47. if e != nil {
  48. t.Error(e)
  49. }
  50. fmt.Printf("decode => %s \n", string(ret))
  51. }