12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- package comm
- import (
- "encoding/hex"
- "fmt"
- "math/rand"
- "testing"
- "time"
- )
- // 长度为62
- var bytes []byte = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890")
- func init() {
- // 保证每次生成的随机数不一样
- rand.Seed(time.Now().UnixNano())
- }
- // 方法一
- func RandStr1(n int) string {
- result := make([]byte, n)
- for i := 0; i < n; i++ {
- result[i] = bytes[rand.Int31()%62]
- }
- return string(result)
- }
- // 方法二
- func RandStr2(n int) string {
- result := make([]byte, n/2)
- rand.Read(result)
- return hex.EncodeToString(result)
- }
- func TestRandom32(t *testing.T) {
- fmt.Println(RandStr1(32))
- }
- func TestAppKey(t *testing.T) {
- appId := "screen"
- appKey := "zjIWxbpOxE9lQaRuiO1lLmed7UqWYVjo"
- secrate := GenSecretKey(appId, appKey)
- t.Error("xxxxx")
- fmt.Printf("appId %s key: %s => %s \n", appId, appKey, secrate)
- }
- func TestEncode(t *testing.T) {
- secrate := "rxk1ly9+IPbBMJkcyUS53wYrh1owMDAwMDAwMDAwMDA="
- c, e := Encode(secrate, []byte("hello world"))
- if e != nil {
- t.Error(e)
- }
- fmt.Printf("encoded => %s \n", c)
- ret, e := Decode(secrate, c)
- if e != nil {
- t.Error(e)
- }
- fmt.Printf("decode => %s \n", string(ret))
- }
|