aboutsummaryrefslogtreecommitdiff
path: root/ecdsa.go
diff options
context:
space:
mode:
authorGibheer <gibheer@gmail.com>2015-02-18 22:55:29 +0100
committerGibheer <gibheer@gmail.com>2015-02-18 22:55:29 +0100
commit639a5379e9abf4a3f0d88464d1a229f8d5df14ae (patch)
tree3d05c242b4eaf8e17548c4ebe877a5765c52c0ed /ecdsa.go
parent577538a5ffb21b402b7ed53279d4b4dce30ace24 (diff)
add sign and verification to ecdsa
This commit adds support to sign and verify messages using ecdsa.
Diffstat (limited to 'ecdsa.go')
-rw-r--r--ecdsa.go25
1 files changed, 21 insertions, 4 deletions
diff --git a/ecdsa.go b/ecdsa.go
index f8f51b2..6754ee4 100644
--- a/ecdsa.go
+++ b/ecdsa.go
@@ -6,8 +6,10 @@ import (
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
+ "encoding/asn1"
"encoding/pem"
"errors"
+ "math/big"
)
const (
@@ -24,6 +26,10 @@ type (
EcdsaPublicKey struct {
public_key *ecdsa.PublicKey
}
+
+ signatureEcdsa struct {
+ R, S *big.Int
+ }
)
// generate a new ecdsa private key
@@ -46,8 +52,14 @@ func (pr EcdsaPrivateKey) Public() PublicKey {
}
// sign a message with the private key
-func (pr EcdsaPrivateKey) Sign(message []byte) ([]byte, error) {
- return make([]byte, 0), errors.New("not implemented yet!")
+func (pr EcdsaPrivateKey) Sign(message []byte, hash crypto.Hash) ([]byte, error) {
+ empty := make([]byte, 0)
+ if !hash.Available() {
+ return empty, errors.New("Hash method is not available!")
+ }
+ hashed_message := hash.New()
+ hashed_message.Write(message)
+ return pr.private_key.Sign(rand.Reader, hashed_message.Sum(nil), hash)
}
// get the private key
@@ -72,6 +84,11 @@ func (pu *EcdsaPublicKey) MarshalPem() (marshalledPemBlock, error) {
}
// verify a message using the ecdsa public key
-func (pu *EcdsaPublicKey) Verify(message []byte, signature []byte) (bool, error) {
- return false, errors.New("not implemented yet!")
+func (pu *EcdsaPublicKey) Verify(message []byte, signature_raw []byte, hash crypto.Hash) (bool, error) {
+ var sig signatureEcdsa
+ _, err := asn1.Unmarshal(signature_raw, &sig)
+ if err != nil { return false, err }
+ hashed_message := hash.New()
+ hashed_message.Write(message)
+ return ecdsa.Verify(pu.public_key, hashed_message.Sum(nil), sig.R, sig.S), nil
}