zzb.go 1.0 KB

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