From 305d16d1dec6956ee2078e3e288445e1bf44f2ce Mon Sep 17 00:00:00 2001 From: Coda Hale Date: Mon, 26 Aug 2013 20:14:56 -0700 Subject: [PATCH] Add examples. EXECUTABLE EXAMPLES Man is that cool. --- README.md | 29 +------------------ example_test.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 28 deletions(-) create mode 100644 example_test.go diff --git a/README.md b/README.md index 8afac7c..2417d45 100644 --- a/README.md +++ b/README.md @@ -4,31 +4,4 @@ rfc6979 A Go implementation of [RFC 6979](https://tools.ietf.org/html/rfc6979)'s deterministic DSA/ECDSA signature scheme. -``` go -package main - -import ( - "crypto/ecdsa" - "crypto/elliptic" - "crypto/rand" - "crypto/sha512" - "fmt" - "github.com/codahale/rfc6979" -) - -func main() { - // Generate a key pair. - // You need a high-quality PRNG for this. - k, _ := ecdsa.GenerateKey(elliptic.P521(), rand.Reader) - - // Hash a message. - alg := sha512.New() - alg.Write([]byte("I am a potato.")) - hash := alg.Sum(nil) - - // Sign the message. You don't need a PRNG for this. - r, s, _ := rfc6979.SignECDSA(k, hash, sha512.New) - fmt.Printf("Signature: %X%X", r, s) -} - -``` +For documentation, check [godoc](http://godoc.org/github.com/codahale/rfc6979). diff --git a/example_test.go b/example_test.go new file mode 100644 index 0000000..cb6056b --- /dev/null +++ b/example_test.go @@ -0,0 +1,76 @@ +package rfc6979 + +import ( + "crypto/ecdsa" + "crypto/sha512" + "crypto/rand" + "crypto/elliptic" + "crypto/dsa" + "crypto/sha1" + "fmt" +) + +// Generates a 521-bit ECDSA key, uses SHA-512 to sign a message, then verifies +// it. +func ExampleSignECDSA() { + // Generate a key pair. + // You need a high-quality PRNG for this. + k, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader) + + if err != nil { + fmt.Println(err) + return + } + + // Hash a message. + alg := sha512.New() + alg.Write([]byte("I am a potato.")) + hash := alg.Sum(nil) + + // Sign the message. You don't need a PRNG for this. + r, s, err := SignECDSA(k, hash, sha512.New) + if err != nil { + fmt.Println(err) + return + } + + if !ecdsa.Verify(&k.PublicKey, hash, r, s) { + fmt.Println("Invalid signature!") + } + + // Output: +} + +// Generates a 1024-bit DSA key, uses SHA-1 to sign a message, then verifies it. +func ExampleSignDSA() { + // Here I'm generating some DSA params, but you should really pre-generate + // these and re-use them, since this takes a long time and isn't necessary. + k := new(dsa.PrivateKey) + dsa.GenerateParameters(&k.Parameters, rand.Reader, dsa.L1024N160) + + // Generate a key pair. + // You need a high-quality PRNG for this. + err := dsa.GenerateKey(k, rand.Reader) + if err != nil { + fmt.Println(err) + return + } + + // Hash a message. + alg := sha1.New() + alg.Write([]byte("I am a potato.")) + hash := alg.Sum(nil) + + // Sign the message. You don't need a PRNG for this. + r, s, err := SignDSA(k, hash, sha1.New) + if err != nil { + fmt.Println(err) + return + } + + if !dsa.Verify(&k.PublicKey, hash, r, s) { + fmt.Println("Invalid signature!") + } + + // Output: +}