package main import ( "bytes" "crypto/rc4" "encoding/hex" "encoding/json" "fmt" "goInOut/consumers/GZG2ZJHL/protoFiles_zjhl" "goInOut/utils" "google.golang.org/protobuf/proto" "log" "sync" "testing" "text/template" "time" ) func TestCRC16CCITT(t *testing.T) { hexs := "0a16080718a8ea8fab06620c0a0471bd54c11204e01d8641" hexs = "0a3508011a1c10c7e84318959a9eb5d332220f69cdcc8c3f15333353401dcdcc0c40721331313131313131313131313131313131313131" Hexdata, _ := hex.DecodeString(hexs) crc := utils.NewCRC16CCITT().GetWCRCin(Hexdata) log.Println(hex.EncodeToString(crc)) log.Println("===1==") needRC4 := append(Hexdata, crc...) rc4Key := []byte("t/500101") dest1 := make([]byte, len(needRC4)) cipher1, _ := rc4.NewCipher(rc4Key) cipher1.XORKeyStream(dest1, needRC4) log.Printf("最终报文 dest1=%s", hex.EncodeToString(dest1)) } func TestCRC16CCITT2(t *testing.T) { //果子沟 hexs := "0a3508011a1c10c7e84318e6b4e3b6d332220f0dcdcc8c3f15333353401dcdcc0c40721331313131313131313131313131313131313131" HexData, _ := hex.DecodeString(hexs) crc := utils.NewCRC16CCITT().GetWCRCin(HexData) log.Println("crc=", hex.EncodeToString(crc)) log.Println("===1==") needRC4 := append(HexData, crc...) rc4Key := []byte("t/gzgyy0219") dest1 := make([]byte, len(needRC4)) cipher1, _ := rc4.NewCipher(rc4Key) cipher1.XORKeyStream(dest1, needRC4) log.Printf("最终报文 dest1=%s", hex.EncodeToString(dest1)) } func TestRC4(t *testing.T) { rc4Key := []byte("t/500101") cipher2, _ := rc4.NewCipher(rc4Key) raw := "0a2f080210f8b6d5d01218b395bffec3313a1e0a08a75fed41a75fed41120892c5d64292c5d6421a08f6c8a74379c9a743864c" log.Println("原始报文 raw=", raw) needRC4New, _ := hex.DecodeString(raw) dest2 := make([]byte, len(needRC4New)) cipher2.XORKeyStream(dest2, needRC4New) log.Printf("最终报文 dest1=%s", hex.EncodeToString(dest2)) } func TestRC42(t *testing.T) { rc4Key := []byte("t/gzgyy0219") cipher2, _ := rc4.NewCipher(rc4Key) raw := "0a3508011a1c10c7e84318959a9eb5d332220f69cdcc8c3f15333353401dcdcc0c407213313131313131313131313131313131313131310ea8" log.Println("原始报文 raw=", raw) needRC4New, _ := hex.DecodeString(raw) dest2 := make([]byte, len(needRC4New)) cipher2.XORKeyStream(dest2, needRC4New) log.Printf("最终报文 dest1=%s", hex.EncodeToString(dest2)) } func TestDeRC4(b *testing.T) { //rawStr := "115 75 64 219 28 135 13 201 130 163 32 82 47 39 63 225 28 87 107 175 184 7 177 47 201 105 189 124 131 171 56 167 196 44 45 48 168 223 209 197 214 169 113 3 71 223 30 246 205 130 44 81 145 106 71 230 225" //var needRC4 []byte //rawStrByte := strings.Split(rawStr, " ") // //for _, bis := range rawStrByte { // distInt, _ := strconv.Atoi(bis) // needRC4 = append(needRC4, byte(distInt)) //} rawNeedRC4 := "735740db1cbe15037a55947d2ea192b2c5d2cc15476b3bf00859bc20ceb6f52b89715f2339aec5f4e7d801e953 " needRC4, _ := hex.DecodeString(rawNeedRC4) println("raw反解析 hex=", rawNeedRC4) rc4KeyStr := "t/gzgyy0219" rc4Key := []byte(rc4KeyStr) //the.RC4Key // 加密操作 dest1 := make([]byte, len(needRC4)) rc4.NewCipher(rc4Key) cipher1, _ := rc4.NewCipher(rc4Key) cipher1.XORKeyStream(dest1, needRC4) rawWithCrc16byte := hex.EncodeToString(dest1) println("带crc", rawWithCrc16byte) RawBytesNoCrc := dest1[:len(dest1)-2] println("无crc", hex.EncodeToString(RawBytesNoCrc)) println("=========") complexData := &protoFiles_zjhl.ComplexData{} err := proto.Unmarshal(RawBytesNoCrc, complexData) if err != nil { println(err.Error()) } bs, _ := json.Marshal(complexData) str := string(bs) println("原结构", str) } func TestDeRC4_2(b *testing.T) { rawNeedRC4 := "735740db1cbe15037a55947d2ea192b2c5d2cc15476b3bf00859bc20ceb6f52b89715f2339aec5f4e7d801e953" needRC4, _ := hex.DecodeString(rawNeedRC4) println("raw反解析 hex=", rawNeedRC4) rc4KeyStr := "t/gzgyy0219" rc4Key := []byte(rc4KeyStr) //the.RC4Key // 加密操作 dest1 := make([]byte, len(needRC4)) rc4.NewCipher(rc4Key) cipher1, _ := rc4.NewCipher(rc4Key) cipher1.XORKeyStream(dest1, needRC4) rawWithCrc16byte := hex.EncodeToString(dest1) println("带crc", rawWithCrc16byte) RawBytesNoCrc := dest1[:len(dest1)-2] println("无crc", hex.EncodeToString(RawBytesNoCrc)) println("=========") complexData := &protoFiles_zjhl.ComplexData{} err := proto.Unmarshal(RawBytesNoCrc, complexData) if err != nil { println(err.Error()) } } func TestProtobufDe(t *testing.T) { s := "0a3508011a1c10c7e84318e6b4e3b6d332220f0dcdcc8c3f15333353401dcdcc0c40721331313131313131313131313131313131313131" bs, _ := hex.DecodeString(s) complexData := &protoFiles_zjhl.ComplexData{} err := proto.Unmarshal(bs, complexData) if err != nil { println(err.Error()) } } func TestTemplate(t *testing.T) { Create := func(name, t string) *template.Template { return template.Must(template.New(name).Parse(t)) } t2 := Create("t2", "什么是快乐星球-Name: {{.Name}}\n") b := &bytes.Buffer{} t2.Execute(b, struct { Name string }{"Jane Doe"}) time.Sleep(time.Second * 1) bs := b.String() log.Println(bs) } func TestRangeMap(t *testing.T) { m := sync.Map{} //存数据 m.Store(1, 1) m.Store(2, 2) m.Store(3, 3) m.Store(4, 4) m.Store(5, 5) //遍历map m.Range(func(key, value interface{}) bool { log.Printf("开始遍历 %v", key) if key.(int) == 2 { return false } fmt.Println(key, value) return true }) }