github.com/unidoc/unidoc@v2.2.0+incompatible/pdf/core/ecb.go (about)

     1  /*
     2   * This file is subject to the terms and conditions defined in
     3   * file 'LICENSE.md', which is part of this source code package.
     4   */
     5  
     6  package core
     7  
     8  import "crypto/cipher"
     9  
    10  // ecb implements an Electronic Codebook encryption mode.
    11  // This mode is used to compute or validate document permissions for R=6.
    12  type ecb struct {
    13  	b         cipher.Block
    14  	blockSize int
    15  }
    16  
    17  func newECB(b cipher.Block) *ecb {
    18  	return &ecb{
    19  		b:         b,
    20  		blockSize: b.BlockSize(),
    21  	}
    22  }
    23  
    24  type ecbEncrypter ecb
    25  
    26  func newECBEncrypter(b cipher.Block) cipher.BlockMode {
    27  	return (*ecbEncrypter)(newECB(b))
    28  }
    29  
    30  func (x *ecbEncrypter) BlockSize() int { return x.blockSize }
    31  
    32  func (x *ecbEncrypter) CryptBlocks(dst, src []byte) {
    33  	if len(src)%x.blockSize != 0 {
    34  		panic("crypto/cipher: input not full blocks")
    35  	}
    36  	if len(dst) < len(src) {
    37  		panic("crypto/cipher: output smaller than input")
    38  	}
    39  	for len(src) > 0 {
    40  		x.b.Encrypt(dst, src[:x.blockSize])
    41  		src = src[x.blockSize:]
    42  		dst = dst[x.blockSize:]
    43  	}
    44  }
    45  
    46  type ecbDecrypter ecb
    47  
    48  func newECBDecrypter(b cipher.Block) cipher.BlockMode {
    49  	return (*ecbDecrypter)(newECB(b))
    50  }
    51  
    52  func (x *ecbDecrypter) BlockSize() int { return x.blockSize }
    53  
    54  func (x *ecbDecrypter) CryptBlocks(dst, src []byte) {
    55  	if len(src)%x.blockSize != 0 {
    56  		panic("crypto/cipher: input not full blocks")
    57  	}
    58  	if len(dst) < len(src) {
    59  		panic("crypto/cipher: output smaller than input")
    60  	}
    61  	for len(src) > 0 {
    62  		x.b.Decrypt(dst, src[:x.blockSize])
    63  		src = src[x.blockSize:]
    64  		dst = dst[x.blockSize:]
    65  	}
    66  }