Skip to main content

crypto

API

Go 语言标准库中的crypto包提供了一系列用于加密、解密、哈希、数字签名等操作的 API。以下是crypto包及其子包中的一些主要 API 和功能:

1. crypto

  • crypto 包本身提供了一些通用的接口和错误类型,例如:
    • crypto.Signer:用于签名的接口。
    • crypto.Decrypter:用于解密的接口。
    • crypto.Hash:枚举类型,表示哈希函数的标识符。
    • crypto.ErrInvalidKey:表示无效密钥的错误。

2. crypto/aes

  • 提供 AES 对称加密算法的实现。
    • aes.NewCipher(key []byte) (cipher.Block, error):创建一个新的 AES 密码块。

3. crypto/cipher

  • 提供分组密码的加密和解密操作。
    • cipher.Block:分组密码的接口。
    • cipher.NewCFBEncrypter(block cipher.Block, iv []byte) cipher.Stream:创建一个新的 CFB 模式加密流。
    • cipher.NewCFBDecrypter(block cipher.Block, iv []byte) cipher.Stream:创建一个新的 CFB 模式解密流。

4. crypto/des

  • 提供 DES 和 3DES 对称加密算法的实现。
    • des.NewCipher(key []byte) (cipher.Block, error):创建一个新的 DES 密码块。
    • des.NewTripleDESCipher(key []byte) (cipher.Block, error):创建一个新的 3DES 密码块。

5. crypto/dsa

  • 提供数字签名算法(DSA)的实现。
    • dsa.GenerateKey(priv *dsa.PrivateKey, rand io.Reader) error:生成 DSA 密钥对。
    • dsa.Sign(rand io.Reader, priv *dsa.PrivateKey, hash []byte) (r, s *big.Int, err error):生成 DSA 签名。
    • dsa.Verify(pub *dsa.PublicKey, hash []byte, r, s *big.Int) bool:验证 DSA 签名。

6. crypto/ecdsa

  • 提供椭圆曲线数字签名算法(ECDSA)的实现。
    • ecdsa.GenerateKey(c elliptic.Curve, rand io.Reader) (*ecdsa.PrivateKey, error):生成 ECDSA 密钥对。
    • ecdsa.Sign(rand io.Reader, priv *ecdsa.PrivateKey, hash []byte) (r, s *big.Int, err error):生成 ECDSA 签名。
    • ecdsa.Verify(pub *ecdsa.PublicKey, hash []byte, r, s *big.Int) bool:验证 ECDSA 签名。

7. crypto/ed25519

  • 提供 Ed25519 签名算法的实现。
    • ed25519.GenerateKey(rand io.Reader) (ed25519.PublicKey, ed25519.PrivateKey, error):生成 Ed25519 密钥对。
    • ed25519.Sign(privateKey ed25519.PrivateKey, message []byte) []byte:生成 Ed25519 签名。
    • ed25519.Verify(publicKey ed25519.PublicKey, message, sig []byte) bool:验证 Ed25519 签名。

8. crypto/hmac

  • 提供基于哈希的消息认证码(HMAC)的实现。
    • hmac.New(hash func() hash.Hash, key []byte) hash.Hash:创建一个新的 HMAC 哈希对象。

9. crypto/md5

  • 提供 MD5 哈希算法的实现。
    • md5.New() hash.Hash:创建一个新的 MD5 哈希对象。
    • md5.Sum(data []byte) [16]byte:计算 MD5 哈希值。

10. crypto/rand

  • 提供用于加密目的的随机数生成器。
    • rand.Read(b []byte) (n int, err error):读取随机数据。

11. crypto/rsa

  • 提供 RSA 加密和签名算法的实现。
    • rsa.GenerateKey(rand io.Reader, bits int) (*rsa.PrivateKey, error):生成 RSA 密钥对。
    • rsa.EncryptPKCS1v15(rand io.Reader, pub *rsa.PublicKey, msg []byte) ([]byte, error):使用公钥加密消息。
    • rsa.DecryptPKCS1v15(rand io.Reader, priv *rsa.PrivateKey, ciphertext []byte) ([]byte, error):使用私钥解密消息。
    • rsa.SignPKCS1v15(rand io.Reader, priv *rsa.PrivateKey, hash crypto.Hash, hashed []byte) ([]byte, error):生成 RSA 签名。
    • rsa.VerifyPKCS1v15(pub *rsa.PublicKey, hash crypto.Hash, hashed []byte, sig []byte) error:验证 RSA 签名。

12. crypto/sha1

  • 提供 SHA-1 哈希算法的实现。
    • sha1.New() hash.Hash:创建一个新的 SHA-1 哈希对象。
    • sha1.Sum(data []byte) [20]byte:计算 SHA-1 哈希值。

13. crypto/sha256

  • 提供 SHA-256 哈希算法的实现。
    • sha256.New() hash.Hash:创建一个新的 SHA-256 哈希对象。
    • sha256.Sum256(data []byte) [32]byte:计算 SHA-256 哈希值。

14. crypto/sha512

  • 提供 SHA-512 哈希算法的实现。
    • sha512.New() hash.Hash:创建一个新的 SHA-512 哈希对象。
    • sha512.Sum512(data []byte) [64]byte:计算 SHA-512 哈希值。

15. crypto/x509

  • 提供 X.509 证书的解析和生成。
    • x509.ParseCertificate(asn1Data []byte) (*x509.Certificate, error):解析 X.509 证书。
    • x509.CreateCertificate(rand io.Reader, template, parent *x509.Certificate, pub interface{}, priv interface{}) (cert []byte, err error):创建 X.509 证书。

这些 API 涵盖了常见的加密、解密、哈希和数字签名操作。可以根据具体需求选择合适的 API 进行实现。