💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# package rsa `import "crypto/rsa"` rsa包实现了PKCS#1规定的RSA加密算法。 ## Index * [Constants](#pkg-constants) * [Variables](#pkg-variables) * [type CRTValue](#CRTValue) * [type PrecomputedValues](#PrecomputedValues) * [type PublicKey](#PublicKey) * [type PrivateKey](#PrivateKey) * [func GenerateKey(random io.Reader, bits int) (priv \*PrivateKey, err error)](#GenerateKey) * [func GenerateMultiPrimeKey(random io.Reader, nprimes int, bits int) (priv \*PrivateKey, err error)](#GenerateMultiPrimeKey) * [func (priv \*PrivateKey) Precompute()](#PrivateKey.Precompute) * [func (priv \*PrivateKey) Validate() error](#PrivateKey.Validate) * [type PSSOptions](#PSSOptions) * [func EncryptOAEP(hash hash.Hash, random io.Reader, pub \*PublicKey, msg []byte, label []byte) (out []byte, err error)](#EncryptOAEP) * [func DecryptOAEP(hash hash.Hash, random io.Reader, priv \*PrivateKey, ciphertext []byte, label []byte) (msg []byte, err error)](#DecryptOAEP) * [func EncryptPKCS1v15(rand io.Reader, pub \*PublicKey, msg []byte) (out []byte, err error)](#EncryptPKCS1v15) * [func DecryptPKCS1v15(rand io.Reader, priv \*PrivateKey, ciphertext []byte) (out []byte, err error)](#DecryptPKCS1v15) * [func DecryptPKCS1v15SessionKey(rand io.Reader, priv \*PrivateKey, ciphertext []byte, key []byte) (err error)](#DecryptPKCS1v15SessionKey) * [func SignPKCS1v15(rand io.Reader, priv \*PrivateKey, hash crypto.Hash, hashed []byte) (s []byte, err error)](#SignPKCS1v15) * [func VerifyPKCS1v15(pub \*PublicKey, hash crypto.Hash, hashed []byte, sig []byte) (err error)](#VerifyPKCS1v15) * [func SignPSS(rand io.Reader, priv \*PrivateKey, hash crypto.Hash, hashed []byte, opts \*PSSOptions) (s []byte, err error)](#SignPSS) * [func VerifyPSS(pub \*PublicKey, hash crypto.Hash, hashed []byte, sig []byte, opts \*PSSOptions) error](#VerifyPSS) ## Constants ``` const ( // PSSSaltLengthAuto让PSS签名在签名时让盐尽可能长,并在验证时自动检测出盐。 PSSSaltLengthAuto = 0 // PSSSaltLengthEqualsHash让盐的长度和用于签名的哈希值的长度相同。 PSSSaltLengthEqualsHash = -1 ) ``` ## Variables ``` var ErrDecryption = errors.New("crypto/rsa: decryption error") ``` ErrDecryption代表解密数据失败。它故意写的语焉不详,以避免适应性攻击。 ``` var ErrMessageTooLong = errors.New("crypto/rsa: message too long for RSA public key size") ``` 当试图用公钥加密尺寸过大的数据时,就会返回ErrMessageTooLong。 ``` var ErrVerification = errors.New("crypto/rsa: verification error") ``` ErrVerification代表认证签名失败。它故意写的语焉不详,以避免适应性攻击。 ## type [CRTValue](https://github.com/golang/go/blob/master/src/crypto/rsa/rsa.go#L73 "View Source") ``` type CRTValue struct { Exp *big.Int // D mod (prime-1). Coeff *big.Int // R·Coeff ≡ 1 mod Prime. R *big.Int // product of primes prior to this (inc p and q). } ``` CRTValue包含预先计算的中国剩余定理的值。 ## type [PrecomputedValues](https://github.com/golang/go/blob/master/src/crypto/rsa/rsa.go#L61 "View Source") ``` type PrecomputedValues struct { Dp, Dq *big.Int // D mod (P-1) (or mod Q-1) Qinv *big.Int // Q^-1 mod P // CRTValues用于保存第3个及其余的素数的预计算值。 // 因为历史原因,头两个素数的CRT在PKCS#1中的处理是不同的。 // 因为互操作性十分重要,我们镜像了这些素数的预计算值。 CRTValues []CRTValue } ``` ## type [PublicKey](https://github.com/golang/go/blob/master/src/crypto/rsa/rsa.go#L21 "View Source") ``` type PublicKey struct { N *big.Int // 模 E int // 公开的指数 } ``` 代表一个RSA公钥。 ## type [PrivateKey](https://github.com/golang/go/blob/master/src/crypto/rsa/rsa.go#L51 "View Source") ``` type PrivateKey struct { PublicKey // 公钥 D *big.Int // 私有的指数 Primes []*big.Int // N的素因子,至少有两个 // 包含预先计算好的值,可在某些情况下加速私钥的操作 Precomputed PrecomputedValues } ``` 代表一个RSA私钥。 ### func [GenerateKey](https://github.com/golang/go/blob/master/src/crypto/rsa/rsa.go#L125 "View Source") ``` func GenerateKey(random io.Reader, bits int) (priv *PrivateKey, err error) ``` GenerateKey函数使用随机数据生成器random生成一对具有指定字位数的RSA密钥。 ### func [GenerateMultiPrimeKey](https://github.com/golang/go/blob/master/src/crypto/rsa/rsa.go#L140 "View Source") ``` func GenerateMultiPrimeKey(random io.Reader, nprimes int, bits int) (priv *PrivateKey, err error) ``` GenerateMultiPrimeKey使用指定的字位数生成一对多质数的RSA密钥,参见US patent 4405829。虽然公钥可以和二质数情况下的公钥兼容(事实上,不能区分两种公钥),私钥却不行。因此有可能无法生成特定格式的多质数的密钥对,或不能将生成的密钥用在其他(语言的)代码里。 [http://www.cacr.math.uwaterloo.ca/techreports/2006/cacr2006-16.pdf](http://www.cacr.math.uwaterloo.ca/techreports/2006/cacr2006-16.pdf)中的Table 1说明了给定字位数的密钥可以接受的质数最大数量。 ### func (\*PrivateKey) [Precompute](https://github.com/golang/go/blob/master/src/crypto/rsa/rsa.go#L348 "View Source") ``` func (priv *PrivateKey) Precompute() ``` Precompute方法会预先进行一些计算,以加速未来的私钥的操作。 ### func (\*PrivateKey) [Validate](https://github.com/golang/go/blob/master/src/crypto/rsa/rsa.go#L81 "View Source") ``` func (priv *PrivateKey) Validate() error ``` Validate方法进行密钥的完整性检查。如果密钥合法会返回nil,否则会返回说明问题的error值。 ## type [PSSOptions](https://github.com/golang/go/blob/master/src/crypto/rsa/pss.go#L220 "View Source") ``` type PSSOptions struct { // SaltLength控制PSS签名中加盐的长度,可以是字节数,或者某个PSS盐长度的常数 SaltLength int } ``` PSSOptions包含用于创建和认证PSS签名的参数。 ## func [EncryptOAEP](https://github.com/golang/go/blob/master/src/crypto/rsa/rsa.go#L268 "View Source") ``` func EncryptOAEP(hash hash.Hash, random io.Reader, pub *PublicKey, msg []byte, label []byte) (out []byte, err error) ``` 采用RSA-OAEP算法加密给出的数据。数据不能超过((公共模数的长度)-2*( hash长度)+2)字节。 ## func [DecryptOAEP](https://github.com/golang/go/blob/master/src/crypto/rsa/rsa.go#L457 "View Source") ``` func DecryptOAEP(hash hash.Hash, random io.Reader, priv *PrivateKey, ciphertext []byte, label []byte) (msg []byte, err error) ``` DecryptOAEP解密RSA-OAEP算法加密的数据。如果random不是nil,函数会注意规避时间侧信道攻击。 ## func [EncryptPKCS1v15](https://github.com/golang/go/blob/master/src/crypto/rsa/pkcs1v15.go#L21 "View Source") ``` func EncryptPKCS1v15(rand io.Reader, pub *PublicKey, msg []byte) (out []byte, err error) ``` EncryptPKCS1v15使用PKCS#1 v1.5规定的填充方案和RSA算法加密msg。信息不能超过((公共模数的长度)-11)字节。注意:使用本函数加密明文(而不是会话密钥)是危险的,请尽量在新协议中使用RSA OAEP。 ## func [DecryptPKCS1v15](https://github.com/golang/go/blob/master/src/crypto/rsa/pkcs1v15.go#L52 "View Source") ``` func DecryptPKCS1v15(rand io.Reader, priv *PrivateKey, ciphertext []byte) (out []byte, err error) ``` DecryptPKCS1v15使用PKCS#1 v1.5规定的填充方案和RSA算法解密密文。如果random不是nil,函数会注意规避时间侧信道攻击。 ## func [DecryptPKCS1v15SessionKey](https://github.com/golang/go/blob/master/src/crypto/rsa/pkcs1v15.go#L80 "View Source") ``` func DecryptPKCS1v15SessionKey(rand io.Reader, priv *PrivateKey, ciphertext []byte, key []byte) (err error) ``` DecryptPKCS1v15SessionKey使用PKCS#1 v1.5规定的填充方案和RSA算法解密会话密钥。如果random不是nil,函数会注意规避时间侧信道攻击。 如果密文长度不对,或者如果密文比公共模数的长度还长,会返回错误;否则,不会返回任何错误。如果填充是合法的,生成的明文信息会拷贝进key;否则,key不会被修改。这些情况都会在固定时间内出现(规避时间侧信道攻击)。本函数的目的是让程序的使用者事先生成一个随机的会话密钥,并用运行时的值继续协议。这样可以避免任何攻击者从明文窃取信息的可能性。 参见”Chosen Ciphertext Attacks Against Protocols Based on the RSA Encryption Standard PKCS #1”。 ## func [SignPKCS1v15](https://github.com/golang/go/blob/master/src/crypto/rsa/pkcs1v15.go#L194 "View Source") ``` func SignPKCS1v15(rand io.Reader, priv *PrivateKey, hash crypto.Hash, hashed []byte) (s []byte, err error) ``` SignPKCS1v15使用RSA PKCS#1 v1.5规定的RSASSA-PKCS1-V1_5-SIGN签名方案计算签名。注意hashed必须是使用提供给本函数的hash参数对(要签名的)原始数据进行hash的结果。 ## func [VerifyPKCS1v15](https://github.com/golang/go/blob/master/src/crypto/rsa/pkcs1v15.go#L231 "View Source") ``` func VerifyPKCS1v15(pub *PublicKey, hash crypto.Hash, hashed []byte, sig []byte) (err error) ``` VerifyPKCS1v15认证RSA PKCS#1 v1.5签名。hashed是使用提供的hash参数对(要签名的)原始数据进行hash的结果。合法的签名会返回nil,否则表示签名不合法。 ## func [SignPSS](https://github.com/golang/go/blob/master/src/crypto/rsa/pss.go#L238 "View Source") ``` func SignPSS(rand io.Reader, priv *PrivateKey, hash crypto.Hash, hashed []byte, opts *PSSOptions) (s []byte, err error) ``` SignPSS采用RSASSA-PSS方案计算签名。注意hashed必须是使用提供给本函数的hash参数对(要签名的)原始数据进行hash的结果。opts参数可以为nil,此时会使用默认参数。 ## func [VerifyPSS](https://github.com/golang/go/blob/master/src/crypto/rsa/pss.go#L259 "View Source") ``` func VerifyPSS(pub *PublicKey, hash crypto.Hash, hashed []byte, sig []byte, opts *PSSOptions) error ``` VerifyPSS认证一个PSS签名。hashed是使用提供给本函数的hash参数对(要签名的)原始数据进行hash的结果。合法的签名会返回nil,否则表示签名不合法。opts参数可以为nil,此时会使用默认参数。