Skip to content

Instantly share code, notes, and snippets.

@fourst4r
Created March 22, 2018 19:32
Show Gist options
  • Save fourst4r/4f1c93918755871238c98921354ac062 to your computer and use it in GitHub Desktop.
Save fourst4r/4f1c93918755871238c98921354ac062 to your computer and use it in GitHub Desktop.
// Useful for AES encryption.
// Pad the ciphertext with zeros until it is of length blockSize.
func ZerosPad(ciphertext []byte, blockSize int) []byte {
// determine number of zeros to add
padLen := blockSize - (len(ciphertext) % blockSize)
padText := bytes.Repeat([]byte{0}, padLen)
ciphertext = append(ciphertext, padText...)
return ciphertext
}
// Trim the trailing zeros from ciphertext.
func ZerosUnpad(ciphertext []byte) []byte {
return bytes.TrimRight(ciphertext, string(byte(0)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment