github.com/sereiner/library@v0.0.0-20200518095232-1fa3e640cc5f/security/des/ecb.crypter.go (about) 1 package des 2 3 import ( 4 "crypto/cipher" 5 ) 6 7 type Ecb struct { 8 B cipher.Block 9 blockSize int 10 } 11 12 func newECB(b cipher.Block) *Ecb { 13 return &Ecb{ 14 B: b, 15 blockSize: b.BlockSize(), 16 } 17 } 18 19 type EcbEncrypter Ecb 20 21 func NewECBEncrypter(b cipher.Block) cipher.BlockMode { 22 return (*EcbEncrypter)(newECB(b)) 23 } 24 25 func (e *EcbEncrypter) BlockSize() int { 26 return e.blockSize 27 } 28 29 func (e *EcbEncrypter) CryptBlocks(dst, src []byte) { 30 if len(src)%e.blockSize != 0 { 31 panic("aesecb: input not full blocks") 32 } 33 if len(dst) < len(src) { 34 panic("aesecb: output smaller than input") 35 } 36 for len(src) > 0 { 37 e.B.Encrypt(dst, src[:e.blockSize]) 38 src = src[e.blockSize:] 39 dst = dst[e.blockSize:] 40 } 41 } 42 43 type EcbDecrypter Ecb 44 45 func NewECBDecrypter(b cipher.Block) cipher.BlockMode { 46 return (*EcbDecrypter)(newECB(b)) 47 } 48 49 func (e *EcbDecrypter) BlockSize() int { 50 return e.blockSize 51 } 52 53 func (e *EcbDecrypter) CryptBlocks(dst, src []byte) { 54 if len(src)%e.blockSize != 0 { 55 panic("aesecb: input not full blocks") 56 } 57 if len(dst) < len(src) { 58 panic("aesecb: output smaller than input") 59 } 60 for len(src) > 0 { 61 e.B.Decrypt(dst, src[:e.blockSize]) 62 src = src[e.blockSize:] 63 dst = dst[e.blockSize:] 64 } 65 }