/
zzc.go
64 lines (53 loc) · 1.5 KB
/
zzc.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
// go官方没有实现ecb加密模式
package security
import (
"crypto/cipher"
)
type ecb struct {
b cipher.Block
blockSize int
}
func newECB(b cipher.Block) *ecb {
return &ecb{
b: b,
blockSize: b.BlockSize(),
}
}
type ecbEncrypter ecb
func NewECBEncrypter(b cipher.Block) cipher.BlockMode {
return (*ecbEncrypter)(newECB(b))
}
func (x *ecbEncrypter) BlockSize() int { return x.blockSize }
func (x *ecbEncrypter) CryptBlocks(dst, src []byte) {
if len(src)%x.blockSize != 0 {
panic("dm/security: input not full blocks")
}
if len(dst) < len(src) {
panic("dm/security: output smaller than input")
}
if InexactOverlap(dst[:len(src)], src) {
panic("dm/security: invalid buffer overlap")
}
for bs, be := 0, x.blockSize; bs < len(src); bs, be = bs+x.blockSize, be+x.blockSize {
x.b.Encrypt(dst[bs:be], src[bs:be])
}
}
type ecbDecrypter ecb
func NewECBDecrypter(b cipher.Block) cipher.BlockMode {
return (*ecbDecrypter)(newECB(b))
}
func (x *ecbDecrypter) BlockSize() int { return x.blockSize }
func (x *ecbDecrypter) CryptBlocks(dst, src []byte) {
if len(src)%x.blockSize != 0 {
panic("github.com/gaozejie/dm/security: input not full blocks")
}
if len(dst) < len(src) {
panic("github.com/gaozejie/dm/security: output smaller than input")
}
if InexactOverlap(dst[:len(src)], src) {
panic("github.com/gaozejie/dm/security: invalid buffer overlap")
}
for bs, be := 0, x.blockSize; bs < len(src); bs, be = bs+x.blockSize, be+x.blockSize {
x.b.Decrypt(dst[bs:be], src[bs:be])
}
}