1 Star 0 Fork 0

h79 / goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ecb.go 1.40 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2023-08-26 00:18 . sm4 支持
package algorithm
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 ecbEncrypt ecb
// NewECBEncrypt returns a BlockMode which encrypts in electronic code book
// mode, using the given Block.
func NewECBEncrypt(b cipher.Block) cipher.BlockMode {
return (*ecbEncrypt)(newECB(b))
}
func (e *ecbEncrypt) BlockSize() int {
return e.blockSize
}
func (e *ecbEncrypt) CryptBlocks(dst, src []byte) {
if len(src)%e.blockSize != 0 {
panic("cipher/ecb: input not full blocks")
}
if len(dst) < len(src) {
panic("cipher/ecb: output smaller than input")
}
for len(src) > 0 {
e.b.Encrypt(dst, src[:e.blockSize])
src = src[e.blockSize:]
dst = dst[e.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 (e *ecbDecrypter) BlockSize() int {
return e.blockSize
}
func (e *ecbDecrypter) CryptBlocks(dst, src []byte) {
if len(src)%e.blockSize != 0 {
panic("cipher/ecb: input not full blocks")
}
if len(dst) < len(src) {
panic("cipher/ecb: output smaller than input")
}
for len(src) > 0 {
e.b.Decrypt(dst, src[:e.blockSize])
src = src[e.blockSize:]
dst = dst[e.blockSize:]
}
}
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.20.65

搜索帮助

53164aa7 5694891 3bd8fe86 5694891