Last active
February 12, 2023 05:10
-
-
Save DeanThompson/17056cc40b4899e3e7f4 to your computer and use it in GitHub Desktop.
Golang AES ecb mode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils | |
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("crypto/cipher: input not full blocks") | |
} | |
if len(dst) < len(src) { | |
panic("crypto/cipher: 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 | |
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("crypto/cipher: input not full blocks") | |
} | |
if len(dst) < len(src) { | |
panic("crypto/cipher: output smaller than input") | |
} | |
for len(src) > 0 { | |
x.b.Decrypt(dst, src[:x.blockSize]) | |
src = src[x.blockSize:] | |
dst = dst[x.blockSize:] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ey guys, anybody has an example where I can see how to use this functionality?