Last active
June 25, 2019 08:05
-
-
Save chonlatee/b158ffbafe414bf0725eaaecad9ef092 to your computer and use it in GitHub Desktop.
sign and verify with existing private key and public key
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const crypto = require('crypto') | |
const { privateKey, publicKey } = crypto.generateKeyPairSync('ec', { | |
namedCurve: 'prime256v1', | |
publicKeyEncoding: { | |
type: 'spki', | |
format: 'pem' | |
}, | |
privateKeyEncoding: { | |
type: 'pkcs8', | |
format: 'pem' | |
} | |
}); | |
console.log(privateKey) | |
console.log(publicKey) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"crypto/ecdsa" | |
"crypto/rand" | |
"crypto/sha256" | |
"crypto/x509" | |
"encoding/pem" | |
"errors" | |
"fmt" | |
"log" | |
) | |
func main() { | |
privKey := []byte("-----BEGIN PRIVATE KEY-----\n" + | |
"MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgHy8ZWd6zIyMDKWPK" + | |
"9DA5tDfpRo+f8TYEgjh88N3yHLOhRANCAASY6Wpd5GnpmWnslyNu9Y8cgQ01uJv3" + | |
"lhAgxC0THjuNOkTKbFeb8oxFqT3VxSNvv0zT8teoX8+CBisUzuZEzsST" + | |
"\n-----END PRIVATE KEY-----") | |
pubKey := []byte("-----BEGIN PUBLIC KEY-----\n" + | |
"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEmOlqXeRp6Zlp7JcjbvWPHIENNbib" + | |
"95YQIMQtEx47jTpEymxXm/KMRak91cUjb79M0/LXqF/PggYrFM7mRM7Ekw==" + | |
"\n-----END PUBLIC KEY-----") | |
privateKey, err := loadPrivateKey(privKey) | |
if err != nil { | |
panic(err) | |
} | |
publicKey, err := loadPublicKey(pubKey) | |
if err != nil { | |
panic(err) | |
} | |
msg := "hello, world" | |
hash := sha256.Sum256([]byte(msg)) | |
r, s, err := ecdsa.Sign(rand.Reader, privateKey, hash[:]) | |
if err != nil { | |
panic(err) | |
} | |
valid := ecdsa.Verify(publicKey, hash[:], r, s) | |
fmt.Println("signature verified:", valid) | |
} | |
// https://thanethomson.com/2018/11/30/validating-ecdsa-signatures-golang/ | |
func loadPrivateKey(privateKey []byte) (*ecdsa.PrivateKey, error) { | |
block, _ := pem.Decode(privateKey) | |
if block == nil || block.Type != "PRIVATE KEY" { | |
log.Fatal("failed to decode PEM block containing private key") | |
return nil, errors.New("Failed to decode PEM private key") | |
} | |
priv, err := x509.ParsePKCS8PrivateKey(block.Bytes) | |
if err != nil { | |
return nil, errors.New("Failed to parse ECDSA private key") | |
} | |
switch priv := priv.(type) { | |
case *ecdsa.PrivateKey: | |
return priv, nil | |
} | |
return nil, errors.New("Unsupported private key type") | |
} | |
// https://thanethomson.com/2018/11/30/validating-ecdsa-signatures-golang/ | |
func loadPublicKey(publicKey []byte) (*ecdsa.PublicKey, error) { | |
block, _ := pem.Decode([]byte(publicKey)) | |
if block == nil || block.Type != "PUBLIC KEY" { | |
return nil, errors.New("Failed to decode PEM public key") | |
} | |
pub, err := x509.ParsePKIXPublicKey(block.Bytes) | |
if err != nil { | |
return nil, errors.New("Failed to parse ECDSA public key") | |
} | |
switch pub := pub.(type) { | |
case *ecdsa.PublicKey: | |
return pub, nil | |
} | |
return nil, errors.New("Unsupported public key type") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment