gitee.com/curryzheng/dm@v0.0.1/security/zzd.go (about)

     1  /*
     2   * Copyright (c) 2000-2018, 达梦数据库有限公司.
     3   * All rights reserved.
     4   */
     5  
     6  // go官方没有实现ecb加密模式
     7  package security
     8  
     9  import (
    10  	"crypto/cipher"
    11  )
    12  
    13  type ecb struct {
    14  	b         cipher.Block
    15  	blockSize int
    16  }
    17  
    18  func newECB(b cipher.Block) *ecb {
    19  	return &ecb{
    20  		b:         b,
    21  		blockSize: b.BlockSize(),
    22  	}
    23  }
    24  
    25  type ecbEncrypter ecb
    26  
    27  func NewECBEncrypter(b cipher.Block) cipher.BlockMode {
    28  	return (*ecbEncrypter)(newECB(b))
    29  }
    30  
    31  func (x *ecbEncrypter) BlockSize() int { return x.blockSize }
    32  
    33  func (x *ecbEncrypter) CryptBlocks(dst, src []byte) {
    34  	if len(src)%x.blockSize != 0 {
    35  		panic("gitee.com/curryzheng/dm/security: input not full blocks")
    36  	}
    37  	if len(dst) < len(src) {
    38  		panic("gitee.com/curryzheng/dm/security: output smaller than input")
    39  	}
    40  	if InexactOverlap(dst[:len(src)], src) {
    41  		panic("gitee.com/curryzheng/dm/security: invalid buffer overlap")
    42  	}
    43  	for bs, be := 0, x.blockSize; bs < len(src); bs, be = bs+x.blockSize, be+x.blockSize {
    44  		x.b.Encrypt(dst[bs:be], src[bs:be])
    45  	}
    46  }
    47  
    48  type ecbDecrypter ecb
    49  
    50  func NewECBDecrypter(b cipher.Block) cipher.BlockMode {
    51  	return (*ecbDecrypter)(newECB(b))
    52  }
    53  
    54  func (x *ecbDecrypter) BlockSize() int { return x.blockSize }
    55  
    56  func (x *ecbDecrypter) CryptBlocks(dst, src []byte) {
    57  	if len(src)%x.blockSize != 0 {
    58  		panic("gitee.com/curryzheng/dm/security: input not full blocks")
    59  	}
    60  	if len(dst) < len(src) {
    61  		panic("gitee.com/curryzheng/dm/security: output smaller than input")
    62  	}
    63  	if InexactOverlap(dst[:len(src)], src) {
    64  		panic("gitee.com/curryzheng/dm/security: invalid buffer overlap")
    65  	}
    66  	for bs, be := 0, x.blockSize; bs < len(src); bs, be = bs+x.blockSize, be+x.blockSize {
    67  		x.b.Decrypt(dst[bs:be], src[bs:be])
    68  	}
    69  }