| | |
| | |
| | |
| |
|
| | package rsa_test |
| |
|
| | import ( |
| | "crypto" |
| | "crypto/aes" |
| | "crypto/cipher" |
| | "crypto/rand" |
| | "crypto/rsa" |
| | "crypto/sha256" |
| | "crypto/x509" |
| | "encoding/hex" |
| | "encoding/pem" |
| | "fmt" |
| | "os" |
| | "strings" |
| | ) |
| |
|
| | func ExampleGenerateKey() { |
| | privateKey, err := rsa.GenerateKey(rand.Reader, 2048) |
| | if err != nil { |
| | fmt.Fprintf(os.Stderr, "Error generating RSA key: %s", err) |
| | return |
| | } |
| |
|
| | der, err := x509.MarshalPKCS8PrivateKey(privateKey) |
| | if err != nil { |
| | fmt.Fprintf(os.Stderr, "Error marshalling RSA private key: %s", err) |
| | return |
| | } |
| |
|
| | fmt.Printf("%s", pem.EncodeToMemory(&pem.Block{ |
| | Type: "PRIVATE KEY", |
| | Bytes: der, |
| | })) |
| | } |
| |
|
| | func ExampleGenerateKey_testKey() { |
| | |
| | |
| | block, _ := pem.Decode([]byte(strings.ReplaceAll( |
| | `-----BEGIN RSA TESTING KEY----- |
| | MIIEowIBAAKCAQEAsPnoGUOnrpiSqt4XynxA+HRP7S+BSObI6qJ7fQAVSPtRkqso |
| | tWxQYLEYzNEx5ZSHTGypibVsJylvCfuToDTfMul8b/CZjP2Ob0LdpYrNH6l5hvFE |
| | 89FU1nZQF15oVLOpUgA7wGiHuEVawrGfey92UE68mOyUVXGweJIVDdxqdMoPvNNU |
| | l86BU02vlBiESxOuox+dWmuVV7vfYZ79Toh/LUK43YvJh+rhv4nKuF7iHjVjBd9s |
| | B6iDjj70HFldzOQ9r8SRI+9NirupPTkF5AKNe6kUhKJ1luB7S27ZkvB3tSTT3P59 |
| | 3VVJvnzOjaA1z6Cz+4+eRvcysqhrRgFlwI9TEwIDAQABAoIBAEEYiyDP29vCzx/+ |
| | dS3LqnI5BjUuJhXUnc6AWX/PCgVAO+8A+gZRgvct7PtZb0sM6P9ZcLrweomlGezI |
| | FrL0/6xQaa8bBr/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MYqv5LlvDaBxVfilE0 |
| | 2riO4p6BaAdvzXjKeRrGNEKoHNBpOSfYCOM16NjL8hIZB1CaV3WbT5oY+jp7Mzd5 |
| | 7d56RZOE+ERK2uz/7JX9VSsM/LbH9pJibd4e8mikDS9ntciqOH/3 |
| | -----END RSA TESTING KEY-----`, "TESTING KEY", "PRIVATE KEY"))) |
| | testRSA2048, _ := x509.ParsePKCS1PrivateKey(block.Bytes) |
| |
|
| | fmt.Println("Private key bit size:", testRSA2048.N.BitLen()) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | func ExampleDecryptPKCS1v15SessionKey() { |
| | |
| | |
| | |
| | key := make([]byte, 32) |
| | if _, err := rand.Read(key); err != nil { |
| | panic("RNG failure") |
| | } |
| |
|
| | rsaCiphertext, _ := hex.DecodeString("aabbccddeeff") |
| |
|
| | if err := rsa.DecryptPKCS1v15SessionKey(nil, rsaPrivateKey, rsaCiphertext, key); err != nil { |
| | |
| | |
| | |
| | |
| | fmt.Fprintf(os.Stderr, "Error from RSA decryption: %s\n", err) |
| | return |
| | } |
| |
|
| | |
| | |
| | block, err := aes.NewCipher(key) |
| | if err != nil { |
| | panic("aes.NewCipher failed: " + err.Error()) |
| | } |
| |
|
| | |
| | |
| | var zeroNonce [12]byte |
| | aead, err := cipher.NewGCM(block) |
| | if err != nil { |
| | panic("cipher.NewGCM failed: " + err.Error()) |
| | } |
| | ciphertext, _ := hex.DecodeString("00112233445566") |
| | plaintext, err := aead.Open(nil, zeroNonce[:], ciphertext, nil) |
| | if err != nil { |
| | |
| | |
| | fmt.Fprintf(os.Stderr, "Error decrypting: %s\n", err) |
| | return |
| | } |
| |
|
| | fmt.Printf("Plaintext: %s\n", plaintext) |
| | } |
| |
|
| | func ExampleSignPKCS1v15() { |
| | message := []byte("message to be signed") |
| |
|
| | |
| | |
| | |
| | |
| | |
| | hashed := sha256.Sum256(message) |
| |
|
| | signature, err := rsa.SignPKCS1v15(nil, rsaPrivateKey, crypto.SHA256, hashed[:]) |
| | if err != nil { |
| | fmt.Fprintf(os.Stderr, "Error from signing: %s\n", err) |
| | return |
| | } |
| |
|
| | fmt.Printf("Signature: %x\n", signature) |
| | } |
| |
|
| | func ExampleVerifyPKCS1v15() { |
| | message := []byte("message to be signed") |
| | signature, _ := hex.DecodeString("ad2766728615cc7a746cc553916380ca7bfa4f8983b990913bc69eb0556539a350ff0f8fe65ddfd3ebe91fe1c299c2fac135bc8c61e26be44ee259f2f80c1530") |
| |
|
| | |
| | |
| | |
| | |
| | |
| | hashed := sha256.Sum256(message) |
| |
|
| | err := rsa.VerifyPKCS1v15(&rsaPrivateKey.PublicKey, crypto.SHA256, hashed[:], signature) |
| | if err != nil { |
| | fmt.Fprintf(os.Stderr, "Error from verification: %s\n", err) |
| | return |
| | } |
| |
|
| | |
| | } |
| |
|
| | func ExampleEncryptOAEP() { |
| | secretMessage := []byte("send reinforcements, we're going to advance") |
| | label := []byte("orders") |
| |
|
| | |
| | |
| | rng := rand.Reader |
| |
|
| | ciphertext, err := rsa.EncryptOAEP(sha256.New(), rng, &test2048Key.PublicKey, secretMessage, label) |
| | if err != nil { |
| | fmt.Fprintf(os.Stderr, "Error from encryption: %s\n", err) |
| | return |
| | } |
| |
|
| | |
| | |
| | fmt.Printf("Ciphertext: %x\n", ciphertext) |
| | } |
| |
|
| | func ExampleDecryptOAEP() { |
| | ciphertext, _ := hex.DecodeString("4d1ee10e8f286390258c51a5e80802844c3e6358ad6690b7285218a7c7ed7fc3a4c7b950fbd04d4b0239cc060dcc7065ca6f84c1756deb71ca5685cadbb82be025e16449b905c568a19c088a1abfad54bf7ecc67a7df39943ec511091a34c0f2348d04e058fcff4d55644de3cd1d580791d4524b92f3e91695582e6e340a1c50b6c6d78e80b4e42c5b4d45e479b492de42bbd39cc642ebb80226bb5200020d501b24a37bcc2ec7f34e596b4fd6b063de4858dbf5a4e3dd18e262eda0ec2d19dbd8e890d672b63d368768360b20c0b6b8592a438fa275e5fa7f60bef0dd39673fd3989cc54d2cb80c08fcd19dacbc265ee1c6014616b0e04ea0328c2a04e73460") |
| | label := []byte("orders") |
| |
|
| | plaintext, err := rsa.DecryptOAEP(sha256.New(), nil, test2048Key, ciphertext, label) |
| | if err != nil { |
| | fmt.Fprintf(os.Stderr, "Error from decryption: %s\n", err) |
| | return |
| | } |
| |
|
| | fmt.Printf("Plaintext: %s\n", plaintext) |
| |
|
| | |
| | |
| | |
| | } |
| |
|