-
Notifications
You must be signed in to change notification settings - Fork 69
/
tea.go
84 lines (75 loc) · 1.82 KB
/
tea.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package dongle
import (
"bytes"
"fmt"
"golang.org/x/crypto/tea"
)
type TeaError struct {
}
func NewTeaError() TeaError {
return TeaError{}
}
func (e TeaError) KeyError(key []byte) error {
return fmt.Errorf("tea: invalid key size %d, the key must be 16 bytes", len(key))
}
func (e TeaError) RoundsError() error {
return fmt.Errorf("tea: invalid rounds, the rounds must be even")
}
var teaError = NewTeaError()
// ByTea encrypts by tea.
func (e Encrypter) ByTea(key []byte, rounds ...int) Encrypter {
if len(e.src) == 0 || e.Error != nil {
return e
}
if len(rounds) == 0 {
// 64 is the standard number of rounds in tea.
rounds = []int{64}
}
if rounds[0]&1 != 0 {
e.Error = teaError.RoundsError()
return e
}
src, size := e.src, tea.BlockSize
if len(src) > size {
src = NewCipher().EmptyPadding(e.src, size)
}
dst, buffer := make([]byte, size), bytes.NewBufferString("")
for _, chunk := range bytesSplit(src, size) {
block, err := tea.NewCipherWithRounds(key, rounds[0])
if err != nil {
e.Error = teaError.KeyError(key)
return e
}
block.Encrypt(dst, chunk)
buffer.Write(dst)
}
e.dst = buffer.Bytes()
return e
}
// ByTea decrypts by tea.
func (d Decrypter) ByTea(key []byte, rounds ...int) Decrypter {
if len(d.src) == 0 || d.Error != nil {
return d
}
if len(rounds) == 0 {
// 64 is the standard number of rounds in tea.
rounds = []int{64}
}
if rounds[0]&1 != 0 {
d.Error = teaError.RoundsError()
return d
}
src, size := d.src, tea.BlockSize
dst, buffer := make([]byte, size), bytes.NewBufferString("")
for _, chunk := range bytesSplit(src, size) {
block, err := tea.NewCipherWithRounds(key, rounds[0])
if err != nil {
d.Error = teaError.KeyError(key)
return d
}
block.Decrypt(dst, chunk)
buffer.Write(dst)
}
d.dst = NewCipher().EmptyUnPadding(buffer.Bytes())
return d
}