Skip to content

Instantly share code, notes, and snippets.

@hansstimer
Created August 29, 2012 19:45
Show Gist options
  • Save hansstimer/3517835 to your computer and use it in GitHub Desktop.
Save hansstimer/3517835 to your computer and use it in GitHub Desktop.

Revisions

  1. hansstimer renamed this gist Aug 29, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. hansstimer created this gist Aug 29, 2012.
    35 changes: 35 additions & 0 deletions rsasign.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    package main

    import (
    "bytes"
    "crypto/rand"
    "crypto/rsa"
    "crypto/sha1"
    "fmt"
    )

    func main() {
    privateKey, err := rsa.GenerateKey(rand.Reader, 512)
    if err != nil {
    fmt.Printf("rsa.GenerateKey: %v\n", err)
    }

    message := "Hello World!"
    messageBytes := bytes.NewBufferString(message)
    sha1 := sha1.New()

    encrypted, err := rsa.EncryptOAEP(sha1, rand.Reader, &privateKey.PublicKey, messageBytes.Bytes(), nil)
    if err != nil {
    fmt.Printf("EncryptOAEP: %s\n", err)
    }

    decrypted, err := rsa.DecryptOAEP(sha1, rand.Reader, privateKey, encrypted, nil)
    if err != nil {
    fmt.Printf("decrypt: %s\n", err)
    }

    decryptedString := bytes.NewBuffer(decrypted).String()
    fmt.Printf("message: %v\n", message)
    fmt.Printf("encrypted: %v\n", encrypted)
    fmt.Printf("decryptedString: %v\n", decryptedString)
    }