企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# package dsa `import "crypto/dsa"` dsa包实现FIPS 186-3定义的数字签名算法(Digital Signature Algorithm),即DSA算法。 ## Index * [Variables](#pkg-variables) * [type ParameterSizes](#ParameterSizes) * [type Parameters](#Parameters) * [type PublicKey](#PublicKey) * [type PrivateKey](#PrivateKey) * [func GenerateParameters(params \*Parameters, rand io.Reader, sizes ParameterSizes) (err error)](#GenerateParameters) * [func GenerateKey(priv \*PrivateKey, rand io.Reader) error](#GenerateKey) * [func Sign(rand io.Reader, priv \*PrivateKey, hash []byte) (r, s \*big.Int, err error)](#Sign) * [func Verify(pub \*PublicKey, hash []byte, r, s \*big.Int) bool](#Verify) ## Variables ``` var ErrInvalidPublicKey = errors.New("crypto/dsa: invalid public key") ``` 非法公钥,FIPS标准的公钥格式是很严格的,但有些实现没这么严格,使用这些实现的公钥时,就会导致这个错误。 ## type [ParameterSizes](https://github.com/golang/go/blob/master/src/crypto/dsa/dsa.go#L40 "View Source") ``` type ParameterSizes int ``` 是DSA参数中的质数可以接受的字位长度的枚举,参见FIPS 186-3 section 4.2。 ``` const ( L1024N160 ParameterSizes = iota L2048N224 L2048N256 L3072N256 ) ``` ## type [Parameters](https://github.com/golang/go/blob/master/src/crypto/dsa/dsa.go#L16 "View Source") ``` type Parameters struct { P, Q, G *big.Int } ``` Parameters代表密钥的域参数,这些参数可以被一组密钥共享,Q的字位长度必须是8的倍数。 ## type [PublicKey](https://github.com/golang/go/blob/master/src/crypto/dsa/dsa.go#L21 "View Source") ``` type PublicKey struct { Parameters Y *big.Int } ``` PublicKey代表一个DSA公钥。 ## type [PrivateKey](https://github.com/golang/go/blob/master/src/crypto/dsa/dsa.go#L27 "View Source") ``` type PrivateKey struct { PublicKey X *big.Int } ``` PrivateKey代表一个DSA私钥。 ## func [GenerateKey](https://github.com/golang/go/blob/master/src/crypto/dsa/dsa.go#L151 "View Source") ## func [GenerateParameters](https://github.com/golang/go/blob/master/src/crypto/dsa/dsa.go#L55 "View Source") ``` func GenerateParameters(params *Parameters, rand io.Reader, sizes ParameterSizes) (err error) ``` GenerateParameters函数随机设置合法的参数到params。即使机器很快,函数也可能会花费很多时间来生成参数。 ``` func GenerateKey(priv *PrivateKey, rand io.Reader) error ``` GenerateKey生成一对公钥和私钥;priv.PublicKey.Parameters字段必须已经(被GenerateParameters函数)设置了合法的参数。 ## func [Sign](https://github.com/golang/go/blob/master/src/crypto/dsa/dsa.go#L194 "View Source") ``` func Sign(rand io.Reader, priv *PrivateKey, hash []byte) (r, s *big.Int, err error) ``` 使用私钥对任意长度的hash值(必须是较大信息的hash结果)进行签名,返回签名结果(一对大整数)。私钥的安全性取决于密码读取器的熵度(随机程度)。 注意根据FIPS 186-3 section 4.6的规定,hash必须被截断到亚组的长度,本函数是不会自己截断的。 ## func [Verify](https://github.com/golang/go/blob/master/src/crypto/dsa/dsa.go#L249 "View Source") ``` func Verify(pub *PublicKey, hash []byte, r, s *big.Int) bool ``` 使用公钥认证hash和两个大整数r、s构成的签名,报告签名是否合法。 注意根据FIPS 186-3 section 4.6的规定,hash必须被截断到亚组的长度,本函数是不会自己截断的。