mirror of
https://github.com/fumiama/gofastTEA.git
synced 2026-06-30 06:50:24 +08:00
feat: add new method ToBytes
This commit is contained in:
34
tea.go
34
tea.go
@@ -11,10 +11,10 @@ type TEA [4]uint32
|
|||||||
//go:nosplit
|
//go:nosplit
|
||||||
func NewTeaCipher(key []byte) (t TEA) {
|
func NewTeaCipher(key []byte) (t TEA) {
|
||||||
if len(key) == 16 {
|
if len(key) == 16 {
|
||||||
t[3] = binary.BigEndian.Uint32(key[12:])
|
t[3] = binary.BigEndian.Uint32(key[12:16])
|
||||||
t[2] = binary.BigEndian.Uint32(key[8:])
|
t[2] = binary.BigEndian.Uint32(key[8:12])
|
||||||
t[1] = binary.BigEndian.Uint32(key[4:])
|
t[1] = binary.BigEndian.Uint32(key[4:8])
|
||||||
t[0] = binary.BigEndian.Uint32(key[0:])
|
t[0] = binary.BigEndian.Uint32(key[0:4])
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -22,10 +22,28 @@ func NewTeaCipher(key []byte) (t TEA) {
|
|||||||
//go:nosplit
|
//go:nosplit
|
||||||
func NewTeaCipherLittleEndian(key []byte) (t TEA) {
|
func NewTeaCipherLittleEndian(key []byte) (t TEA) {
|
||||||
if len(key) == 16 {
|
if len(key) == 16 {
|
||||||
t[3] = binary.LittleEndian.Uint32(key[12:])
|
t[3] = binary.LittleEndian.Uint32(key[12:16])
|
||||||
t[2] = binary.LittleEndian.Uint32(key[8:])
|
t[2] = binary.LittleEndian.Uint32(key[8:12])
|
||||||
t[1] = binary.LittleEndian.Uint32(key[4:])
|
t[1] = binary.LittleEndian.Uint32(key[4:8])
|
||||||
t[0] = binary.LittleEndian.Uint32(key[0:])
|
t[0] = binary.LittleEndian.Uint32(key[0:4])
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t TEA) ToBytes() []byte {
|
||||||
|
var buf [16]byte
|
||||||
|
binary.BigEndian.PutUint32(buf[0:4], t[0])
|
||||||
|
binary.BigEndian.PutUint32(buf[4:8], t[1])
|
||||||
|
binary.BigEndian.PutUint32(buf[8:12], t[2])
|
||||||
|
binary.BigEndian.PutUint32(buf[12:16], t[3])
|
||||||
|
return buf[:]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t TEA) ToBytesLittleEndian() []byte {
|
||||||
|
var buf [16]byte
|
||||||
|
binary.LittleEndian.PutUint32(buf[0:4], t[0])
|
||||||
|
binary.LittleEndian.PutUint32(buf[4:8], t[1])
|
||||||
|
binary.LittleEndian.PutUint32(buf[8:12], t[2])
|
||||||
|
binary.LittleEndian.PutUint32(buf[12:16], t[3])
|
||||||
|
return buf[:]
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user