mirror of
https://github.com/cwinfo/matterbridge.git
synced 2024-11-14 20:10:25 +00:00
69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
package cryptoutil
|
|
|
|
import (
|
|
"crypto/cipher"
|
|
)
|
|
|
|
// From this code review: https://codereview.appspot.com/7860047/
|
|
// by fasmat for the Go crypto/cipher package
|
|
|
|
type ecb struct {
|
|
b cipher.Block
|
|
blockSize int
|
|
}
|
|
|
|
func newECB(b cipher.Block) *ecb {
|
|
return &ecb{
|
|
b: b,
|
|
blockSize: b.BlockSize(),
|
|
}
|
|
}
|
|
|
|
type ecbEncrypter ecb
|
|
|
|
// NewECBEncrypter returns a BlockMode which encrypts in electronic code book
|
|
// mode, using the given Block.
|
|
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("cryptoutil/ecb: input not full blocks")
|
|
}
|
|
if len(dst) < len(src) {
|
|
panic("cryptoutil/ecb: output smaller than input")
|
|
}
|
|
for len(src) > 0 {
|
|
x.b.Encrypt(dst, src[:x.blockSize])
|
|
src = src[x.blockSize:]
|
|
dst = dst[x.blockSize:]
|
|
}
|
|
}
|
|
|
|
type ecbDecrypter ecb
|
|
|
|
// newECBDecrypter returns a BlockMode which decrypts in electronic code book
|
|
// mode, using the given Block.
|
|
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("cryptoutil/ecb: input not full blocks")
|
|
}
|
|
if len(dst) < len(src) {
|
|
panic("cryptoutil/ecb: output smaller than input")
|
|
}
|
|
for len(src) > 0 {
|
|
x.b.Decrypt(dst, src[:x.blockSize])
|
|
src = src[x.blockSize:]
|
|
dst = dst[x.blockSize:]
|
|
}
|
|
}
|