123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- package xtea
- import "strconv"
- const BlockSize = 8
- type Cipher struct {
-
- table [64]uint32
- }
- type KeySizeError int
- func (k KeySizeError) Error() string {
- return "crypto/xtea: invalid key size " + strconv.Itoa(int(k))
- }
- func NewCipher(key []byte) (*Cipher, error) {
- k := len(key)
- switch k {
- default:
- return nil, KeySizeError(k)
- case 16:
- break
- }
- c := new(Cipher)
- initCipher(c, key)
- return c, nil
- }
- func (c *Cipher) BlockSize() int { return BlockSize }
- func (c *Cipher) Encrypt(dst, src []byte) { encryptBlock(c, dst, src) }
- func (c *Cipher) Decrypt(dst, src []byte) { decryptBlock(c, dst, src) }
- func initCipher(c *Cipher, key []byte) {
-
- var k [4]uint32
- for i := 0; i < len(k); i++ {
- j := i << 2
- k[i] = uint32(key[j+0])<<24 | uint32(key[j+1])<<16 | uint32(key[j+2])<<8 | uint32(key[j+3])
- }
-
- const delta = 0x9E3779B9
- var sum uint32
-
- for i := 0; i < numRounds; {
- c.table[i] = sum + k[sum&3]
- i++
- sum += delta
- c.table[i] = sum + k[(sum>>11)&3]
- i++
- }
- }
|