Last active
September 17, 2018 09:21
-
-
Save littlecxm/2677881058ae9b2854450fee98d5c750 to your computer and use it in GitHub Desktop.
ecb.go
This file contains hidden or 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
// Copyright 2013 The Go Authors. All rights reserved. | |
// Use of this source code is governed by a BSD-style | |
// license that can be found in the LICENSE file. | |
// Electronic Code Book (ECB) mode. | |
// ECB provides confidentiality by assigning a fixed ciphertext block to each | |
// plaintext block. | |
// See NIST SP 800-38A, pp 08-09 | |
package cipher | |
type ecb struct { | |
b Block | |
blockSize int | |
} | |
func newECB(b 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 Block) 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 | |
// NewECBDecrypter returns a BlockMode which decrypts in electronic code book | |
// mode, using the given Block. | |
func NewECBDecrypter(b Block) 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