elgamal

package
v1.0.2023 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 22, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// 默认为 pkcs1 模式
	MarshalPublicKey = MarshalPKCS1PublicKey
	ParsePublicKey   = ParsePKCS1PublicKey

	MarshalPrivateKey = MarshalPKCS1PrivateKey
	ParsePrivateKey   = ParsePKCS1PrivateKey
)
View Source
var ErrCipherLarge = errors.New("elgamal: cipher is larger than public key size")
View Source
var ErrMessageLarge = errors.New("elgamal: message is larger than public key size")

Functions

func Decrypt added in v1.0.2019

func Decrypt(priv *PrivateKey, cipher1, cipher2 []byte) ([]byte, error)

解密

func DecryptAsn1 added in v1.0.2019

func DecryptAsn1(priv *PrivateKey, cipherData []byte) ([]byte, error)

解密 Asn1

func Encrypt added in v1.0.2019

func Encrypt(random io.Reader, pub *PublicKey, message []byte) ([]byte, []byte, error)

加密

func EncryptAsn1 added in v1.0.2019

func EncryptAsn1(random io.Reader, pub *PublicKey, message []byte) ([]byte, error)

加密 Asn1

func GeneratePQZp

func GeneratePQZp(random io.Reader, n, probability int) (*big.Int, *big.Int, *big.Int, error)

Gen emit <p,q,g>. p = 2q + 1, p,q - safe primes g - cyclic group generator Zp performs n Miller-Rabin tests with 1 - 1/(4^n) probability false rate. Gain n - bit width for integer & probability rang for MR. It returns p, q, g and write error message.

func MarshalPKCS1PrivateKey

func MarshalPKCS1PrivateKey(key *PrivateKey) ([]byte, error)

包装私钥

func MarshalPKCS1PublicKey

func MarshalPKCS1PublicKey(key *PublicKey) ([]byte, error)

包装公钥

func MarshalPKCS8PrivateKey

func MarshalPKCS8PrivateKey(key *PrivateKey) ([]byte, error)

PKCS8 包装私钥

func MarshalPKCS8PublicKey

func MarshalPKCS8PublicKey(pub *PublicKey) ([]byte, error)

PKCS8 包装公钥

func MarshalXMLPrivateKey

func MarshalXMLPrivateKey(key *PrivateKey) ([]byte, error)

func MarshalXMLPublicKey

func MarshalXMLPublicKey(key *PublicKey) ([]byte, error)

func Sign

func Sign(rand io.Reader, priv *PrivateKey, hash []byte) ([]byte, []byte, error)

Sign hash

func SignASN1

func SignASN1(rand io.Reader, priv *PrivateKey, hash []byte) ([]byte, error)

SignASN1 signs a hash (which should be the result of hashing a larger message) using the private key, priv. If the hash is longer than the bit-length of the private key's curve order, the hash will be truncated to that length. It returns the ASN.1 encoded signature.

func Verify

func Verify(pub *PublicKey, hash, r, s []byte) (bool, error)

Verify hash

func VerifyASN1

func VerifyASN1(pub *PublicKey, hash, sig []byte) (bool, error)

VerifyASN1 verifies the ASN.1 encoded signature, sig, of hash using the public key, pub. Its return value records whether the signature is valid.

Types

type PKCS1Key

type PKCS1Key struct{}

*

  • elgamal pkcs1 密钥 *
  • @create 2023-6-16
  • @author deatil

func NewPKCS1Key

func NewPKCS1Key() PKCS1Key

构造函数

func (PKCS1Key) MarshalPrivateKey

func (this PKCS1Key) MarshalPrivateKey(key *PrivateKey) ([]byte, error)

包装私钥

func (PKCS1Key) MarshalPublicKey

func (this PKCS1Key) MarshalPublicKey(key *PublicKey) ([]byte, error)

包装公钥

func (PKCS1Key) ParsePrivateKey

func (this PKCS1Key) ParsePrivateKey(der []byte) (*PrivateKey, error)

解析私钥

func (PKCS1Key) ParsePublicKey

func (this PKCS1Key) ParsePublicKey(der []byte) (*PublicKey, error)

解析公钥

type PKCS8Key

type PKCS8Key struct{}

*

  • elgamal pkcs8 密钥 *
  • @create 2023-6-16
  • @author deatil

func NewPKCS8Key

func NewPKCS8Key() PKCS8Key

构造函数

func (PKCS8Key) MarshalPrivateKey

func (this PKCS8Key) MarshalPrivateKey(key *PrivateKey) ([]byte, error)

PKCS8 包装私钥

func (PKCS8Key) MarshalPublicKey

func (this PKCS8Key) MarshalPublicKey(key *PublicKey) ([]byte, error)

PKCS8 包装公钥

func (PKCS8Key) ParsePrivateKey

func (this PKCS8Key) ParsePrivateKey(der []byte) (key *PrivateKey, err error)

PKCS8 解析私钥

func (PKCS8Key) ParsePublicKey

func (this PKCS8Key) ParsePublicKey(der []byte) (*PublicKey, error)

PKCS8 解析公钥

type PrivateKey

type PrivateKey struct {
	PublicKey
	X *big.Int
}

PrivateKey represents Elgamal private key.

func GenerateKey

func GenerateKey(random io.Reader, bitsize, probability int) (*PrivateKey, error)

GenerateKey generates elgamal private key according to given bit size and probability. Moreover, the given probability value is used in choosing prime number P for performing n Miller-Rabin tests with 1 - 1/(4^n) probability false rate.

func ParsePKCS1PrivateKey

func ParsePKCS1PrivateKey(derBytes []byte) (*PrivateKey, error)

解析私钥

func ParsePKCS8PrivateKey

func ParsePKCS8PrivateKey(derBytes []byte) (key *PrivateKey, err error)

PKCS8 解析私钥

func ParseXMLPrivateKey

func ParseXMLPrivateKey(der []byte) (*PrivateKey, error)

func (*PrivateKey) Decrypt

func (priv *PrivateKey) Decrypt(cipher1, cipher2 []byte) ([]byte, error)

Decrypt decrypts the passed cipher text. It returns an error if cipher text value is larger than modulus P of Public key.

func (*PrivateKey) DecryptAsn1

func (priv *PrivateKey) DecryptAsn1(cipherData []byte) ([]byte, error)

Decrypt decrypts the passed cipher text.

func (*PrivateKey) Equal

func (priv *PrivateKey) Equal(x crypto.PrivateKey) bool

Equal reports whether priv and x have the same value.

func (*PrivateKey) Public

func (priv *PrivateKey) Public() crypto.PublicKey

Public returns the public key corresponding to priv.

func (*PrivateKey) Sign

func (priv *PrivateKey) Sign(random io.Reader, hash []byte) ([]byte, []byte, error)

Signature generates signature over the given hash. It returns signature value consisting of two parts "r" and "s" as byte arrays.

type PublicKey

type PublicKey struct {
	G, P, Y *big.Int
}

PublicKey represents a Elgamal public key.

func ParsePKCS1PublicKey

func ParsePKCS1PublicKey(derBytes []byte) (*PublicKey, error)

解析公钥

func ParsePKCS8PublicKey

func ParsePKCS8PublicKey(derBytes []byte) (*PublicKey, error)

PKCS8 解析公钥

func ParseXMLPublicKey

func ParseXMLPublicKey(der []byte) (*PublicKey, error)

func (*PublicKey) Encrypt

func (pub *PublicKey) Encrypt(random io.Reader, message []byte) ([]byte, []byte, error)

Encrypt encrypts a plain text represented as a byte array. It returns an error if plain text value is larger than modulus P of Public key.

func (*PublicKey) EncryptAsn1

func (pub *PublicKey) EncryptAsn1(random io.Reader, message []byte) ([]byte, error)

Encrypt encrypts a plain text represented as a byte array.

func (*PublicKey) Equal

func (pub *PublicKey) Equal(x crypto.PublicKey) bool

Equal reports whether pub and x have the same value.

func (*PublicKey) HommorphicEncMultiple

func (pub *PublicKey) HommorphicEncMultiple(ciphertext [][2][]byte) ([]byte, []byte, error)

HommorphicEncMultiple performs homomorphic operation over multiple passed chiphers. Elgamal has multiplicative homomorphic property, so resultant cipher contains the product of multiple numbers.

func (*PublicKey) HomomorphicEncTwo

func (pub *PublicKey) HomomorphicEncTwo(c1, c2, c1dash, c2dash []byte) ([]byte, []byte, error)

HomomorphicEncTwo performs homomorphic operation over two passed chiphers. Elgamal has multiplicative homomorphic property, so resultant cipher contains the product of two numbers.

func (*PublicKey) Verify

func (pub *PublicKey) Verify(hash, r, s []byte) (bool, error)

Verify verifies signature over the given hash and signature values (r & s). It returns true as a boolean value if signature is verify correctly. Otherwise it returns false along with error message.

type XMLKey

type XMLKey struct{}

*

  • elgamal xml密钥 *
  • @create 2023-6-16
  • @author deatil

func NewXMLKey

func NewXMLKey() XMLKey

构造函数

func (XMLKey) MarshalPrivateKey

func (this XMLKey) MarshalPrivateKey(key *PrivateKey) ([]byte, error)

包装私钥

func (XMLKey) MarshalPublicKey

func (this XMLKey) MarshalPublicKey(key *PublicKey) ([]byte, error)

包装公钥

func (XMLKey) ParsePrivateKey

func (this XMLKey) ParsePrivateKey(data []byte) (*PrivateKey, error)

解析私钥

func (XMLKey) ParsePublicKey

func (this XMLKey) ParsePublicKey(data []byte) (*PublicKey, error)

解析公钥

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL