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.
47 lines
989 B
47 lines
989 B
2 weeks ago
|
package utils
|
||
|
|
||
|
import (
|
||
|
"crypto/rc4"
|
||
|
"encoding/binary"
|
||
|
"log"
|
||
|
"math"
|
||
|
)
|
||
|
|
||
|
func RC4Encode(key string, rawBytes []byte) []byte {
|
||
|
rc4Key := []byte(key)
|
||
|
// 要加密的源数据
|
||
|
dest := make([]byte, len(rawBytes))
|
||
|
cipher1, _ := rc4.NewCipher(rc4Key)
|
||
|
cipher1.XORKeyStream(dest, rawBytes)
|
||
|
log.Printf("RC4加密后:%s ", dest)
|
||
|
return dest
|
||
|
}
|
||
|
|
||
|
func RC4Decode(key string, rawBytes []byte) []byte {
|
||
|
rc4Key := []byte(key)
|
||
|
// 要解密的源数据
|
||
|
dest := make([]byte, len(rawBytes))
|
||
|
cipher, _ := rc4.NewCipher(rc4Key)
|
||
|
cipher.XORKeyStream(dest, rawBytes)
|
||
|
log.Printf("RC4解密后:%s ", dest)
|
||
|
return dest
|
||
|
}
|
||
|
|
||
|
func CRC16CCITTEncode(rawBytes []byte) []byte {
|
||
|
return NewCRC16CCITT().GetWCRCin(rawBytes)
|
||
|
}
|
||
|
|
||
|
func LimitStringLength(s string, maxLength int) string {
|
||
|
if len(s) <= maxLength {
|
||
|
return s
|
||
|
}
|
||
|
return s[:maxLength]
|
||
|
}
|
||
|
|
||
|
func Float32ToBytes(float float32) []byte {
|
||
|
bits := math.Float32bits(float)
|
||
|
bytes := make([]byte, 4)
|
||
|
binary.LittleEndian.PutUint32(bytes, bits)
|
||
|
return bytes
|
||
|
}
|