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

     1  /*
     2   * Copyright (c) 2000-2018, 达梦数据库有限公司.
     3   * All rights reserved.
     4   */
     5  
     6  package security
     7  
     8  import "math/big"
     9  
    10  type DhKey struct {
    11  	x     *big.Int
    12  	y     *big.Int
    13  	group *dhGroup
    14  }
    15  
    16  func newPublicKey(s []byte) *DhKey {
    17  	key := new(DhKey)
    18  	key.y = new(big.Int).SetBytes(s)
    19  	return key
    20  }
    21  
    22  func (dk *DhKey) GetX() *big.Int {
    23  	x := new(big.Int)
    24  	x.Set(dk.x)
    25  	return x
    26  }
    27  
    28  func (dk *DhKey) GetY() *big.Int {
    29  	y := new(big.Int)
    30  	y.Set(dk.y)
    31  	return y
    32  }
    33  
    34  func (dk *DhKey) GetYBytes() []byte {
    35  	if dk.y == nil {
    36  		return nil
    37  	}
    38  	if dk.group != nil {
    39  		blen := (dk.group.p.BitLen() + 7) / 8
    40  		ret := make([]byte, blen)
    41  		copyWithLeftPad(ret, dk.y.Bytes())
    42  		return ret
    43  	}
    44  	return dk.y.Bytes()
    45  }
    46  
    47  func (dk *DhKey) GetYString() string {
    48  	if dk.y == nil {
    49  		return ""
    50  	}
    51  	return dk.y.String()
    52  }
    53  
    54  func (dk *DhKey) IsPrivateKey() bool {
    55  	return dk.x != nil
    56  }
    57  
    58  func copyWithLeftPad(dest, src []byte) {
    59  	numPaddingBytes := len(dest) - len(src)
    60  	for i := 0; i < numPaddingBytes; i++ {
    61  		dest[i] = 0
    62  	}
    63  	copy(dest[:numPaddingBytes], src)
    64  }