github.com/fumiama/gofastTEA@v0.0.10/tea_1.16_amd64.go (about)

     1  //go:build !go1.17 && amd64
     2  // +build !go1.17,amd64
     3  
     4  package tea
     5  
     6  import (
     7  	"encoding/binary"
     8  	"unsafe"
     9  )
    10  
    11  // implemented in tea_$GOARCH.s
    12  func encrypt(dstlen uintptr, tlen uintptr)
    13  func decrypt(datalen uintptr, dstlen uintptr, t *TEA)
    14  
    15  //go:nosplit
    16  func (t TEA) Encrypt(src []byte) (dst []byte) {
    17  	lens := len(src)
    18  	fill := 10 - (lens+1)&7
    19  	dst = make([]byte, fill+lens+7)
    20  	binary.LittleEndian.PutUint32(dst, randuint32())
    21  	binary.LittleEndian.PutUint32(dst[4:], randuint32())
    22  	binary.LittleEndian.PutUint32(dst[8:], randuint32())
    23  	dst[0] = byte(fill-3) | 0xF8 // 存储pad长度
    24  	copy(dst[fill:], src)
    25  	encrypt(uintptr(*(*unsafe.Pointer)(unsafe.Pointer(&dst)))|uintptr(len(dst)<<40), uintptr(unsafe.Pointer(&t))|(uintptr(len(dst)<<16)&0xffffff00_00000000))
    26  	return dst
    27  }
    28  
    29  //go:nosplit
    30  func (t TEA) EncryptTo(src []byte, dst []byte) int {
    31  	lens := len(src)
    32  	fill := 10 - (lens+1)&7
    33  	binary.LittleEndian.PutUint32(dst, randuint32())
    34  	binary.LittleEndian.PutUint32(dst[4:], randuint32())
    35  	binary.LittleEndian.PutUint32(dst[8:], randuint32())
    36  	dst[0] = byte(fill-3) | 0xF8 // 存储pad长度
    37  	copy(dst[fill:], src)
    38  	dstlen := fill + lens + 7
    39  	encrypt(uintptr(*(*unsafe.Pointer)(unsafe.Pointer(&dst)))|uintptr(dstlen<<40), uintptr(unsafe.Pointer(&t))|(uintptr(dstlen<<16)&0xffffff00_00000000))
    40  	return dstlen
    41  }
    42  
    43  /*
    44  //go:nosplit
    45  func (t TEA) Decrypt(data []byte) []byte {
    46  	if len(data) < 16 || len(data)&7 != 0 {
    47  		return nil
    48  	}
    49  	dst := make([]byte, len(data))
    50  	decrypt(uintptr(*(*unsafe.Pointer)(unsafe.Pointer(&data)))|uintptr(len(data)<<40), uintptr(*(*unsafe.Pointer)(unsafe.Pointer(&dst)))|(uintptr(len(data)<<16)&0xffffff00_00000000), &t)
    51  	return dst[dst[0]&7+3 : len(dst)-7]
    52  }
    53  */