本文整理汇总了Golang中crypto/ecdsa.Verify函数的典型用法代码示例。如果您正苦于以下问题:Golang Verify函数的具体用法?Golang Verify怎么用?Golang Verify使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Verify函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: BenchmarkSigVerify
// BenchmarkSigVerify benchmarks how long it takes the secp256k1 curve to
// verify signatures.
func BenchmarkSigVerify(b *testing.B) {
b.StopTimer()
// Randomly generated keypair.
// Private key: 9e0699c91ca1e3b7e3c9ba71eb71c89890872be97576010fe593fbf3fd57e66d
pubKey := ecdsa.PublicKey{
Curve: btcec.S256(),
X: fromHex("d2e670a19c6d753d1a6d8b20bd045df8a08fb162cf508956c31268c6d81ffdab"),
Y: fromHex("ab65528eefbb8057aa85d597258a3fbd481a24633bc9b47a9aa045c91371de52"),
}
// Double sha256 of []byte{0x01, 0x02, 0x03, 0x04}
msgHash := fromHex("8de472e2399610baaa7f84840547cd409434e31f5d3bd71e4d947f283874f9c0")
sigR := fromHex("fef45d2892953aa5bbcdb057b5e98b208f1617a7498af7eb765574e29b5d9c2c")
sigS := fromHex("d47563f52aac6b04b55de236b7c515eb9311757db01e02cff079c3ca6efb063f")
if !ecdsa.Verify(&pubKey, msgHash.Bytes(), sigR, sigS) {
b.Errorf("Signature failed to verify")
return
}
b.StartTimer()
for i := 0; i < b.N; i++ {
ecdsa.Verify(&pubKey, msgHash.Bytes(), sigR, sigS)
}
}
开发者ID:GeertJohan,项目名称:btcec,代码行数:27,代码来源:bench_test.go
示例2: main
func main() {
c := elliptic.P521()
sec, _ := ecdsa.GenerateKey(c, rand.Reader)
pub := &sec.PublicKey
log.Print("pub", pub)
log.Print("sec", sec)
pempub := exportPublicKeytoPEM(pub)
pemsec := exportPrivateKeytoEncryptedPEM(sec, []byte("asdfgh"))
log.Print("pempub", pempub)
log.Print("pemsec", pemsec)
pub = importPublicKeyfromPEM(pempub)
//sec = importPrivateKeyfromPEM(pemsec)
sec = importPrivateKeyfromEncryptedPEM(pemsec, []byte("asdfgh"))
log.Print("pub", pub)
log.Print("sec", sec)
t := sha1.New()
io.WriteString(t, "data") // when msg is a string
//t.Write([]byte("data")) // when msg is []bye
sum1 := t.Sum(nil)[:]
r, s, _ := ecdsa.Sign(rand.Reader, sec, sum1)
log.Printf("r=%d\ts=%d", r, s)
b := ecdsa.Verify(pub, sum1, r, s)
log.Printf("b=%v", b)
b = ecdsa.Verify(pub, sum1, s, r)
log.Printf("b=%v", b)
}
开发者ID:hyg,项目名称:go.sample,代码行数:33,代码来源:main.go
示例3: TestVectors
func TestVectors(t *testing.T) {
sha := sha1.New()
for i, test := range testVectors {
pub := ecdsa.PublicKey{
Curve: btcec.S256(),
X: fromHex(test.Qx),
Y: fromHex(test.Qy),
}
msg, _ := hex.DecodeString(test.msg)
sha.Reset()
sha.Write(msg)
hashed := sha.Sum(nil)
r := fromHex(test.r)
s := fromHex(test.s)
if fuck := ecdsa.Verify(&pub, hashed, r, s); fuck != test.ok {
//t.Errorf("%d: bad result %v %v", i, pub, hashed)
t.Errorf("%d: bad result %v instead of %v", i, fuck,
test.ok)
}
if testing.Short() {
break
}
}
}
开发者ID:hsk81,项目名称:btcec,代码行数:25,代码来源:btcec_test.go
示例4: VerifySignature
// VerifySignature returns nil iff sig is a valid signature, made by this
// public key, of the data hashed into signed. signed is mutated by this call.
func (pk *PublicKey) VerifySignature(signed hash.Hash, sig *Signature) (err error) {
if !pk.CanSign() {
return errors.InvalidArgumentError("public key cannot generate signatures")
}
signed.Write(sig.HashSuffix)
hashBytes := signed.Sum(nil)
// NOTE(maxtaco) 2016-08-22
//
// We used to do this:
//
// if hashBytes[0] != sig.HashTag[0] || hashBytes[1] != sig.HashTag[1] {
// return errors.SignatureError("hash tag doesn't match")
// }
//
// But don't do anything in this case. Some GPGs generate bad
// 2-byte hash prefixes, but GPG also doesn't seem to care on
// import. See BrentMaxwell's key. I think it's safe to disable
// this check!
if pk.PubKeyAlgo != sig.PubKeyAlgo {
return errors.InvalidArgumentError("public key and signature use different algorithms")
}
switch pk.PubKeyAlgo {
case PubKeyAlgoRSA, PubKeyAlgoRSASignOnly:
rsaPublicKey, _ := pk.PublicKey.(*rsa.PublicKey)
err = rsa.VerifyPKCS1v15(rsaPublicKey, sig.Hash, hashBytes, sig.RSASignature.bytes)
if err != nil {
return errors.SignatureError("RSA verification failure")
}
return nil
case PubKeyAlgoDSA:
dsaPublicKey, _ := pk.PublicKey.(*dsa.PublicKey)
// Need to truncate hashBytes to match FIPS 186-3 section 4.6.
subgroupSize := (dsaPublicKey.Q.BitLen() + 7) / 8
if len(hashBytes) > subgroupSize {
hashBytes = hashBytes[:subgroupSize]
}
if !dsa.Verify(dsaPublicKey, hashBytes, new(big.Int).SetBytes(sig.DSASigR.bytes), new(big.Int).SetBytes(sig.DSASigS.bytes)) {
return errors.SignatureError("DSA verification failure")
}
return nil
case PubKeyAlgoECDSA:
ecdsaPublicKey := pk.PublicKey.(*ecdsa.PublicKey)
if !ecdsa.Verify(ecdsaPublicKey, hashBytes, new(big.Int).SetBytes(sig.ECDSASigR.bytes), new(big.Int).SetBytes(sig.ECDSASigS.bytes)) {
return errors.SignatureError("ECDSA verification failure")
}
return nil
case PubKeyAlgoEdDSA:
if !pk.edk.Verify(hashBytes, sig.EdDSASigR, sig.EdDSASigS) {
return errors.SignatureError("EdDSA verification failure")
}
return nil
default:
return errors.SignatureError("Unsupported public key algorithm used in signature")
}
panic("unreachable")
}
开发者ID:quixoten,项目名称:vault,代码行数:62,代码来源:public_key.go
示例5: FinishKEX
// FinishKEX verifies the signed public key. If it is valid, it will
// carry out an ECDH key agreement, zeroise the private key, and store
// the shared key.
func (kex *Session) FinishKEX(signer *ecdsa.PublicKey, signed []byte) error {
var skey signedKey
rest, err := asn1.Unmarshal(signed, &skey)
if err != nil {
return err
} else if len(rest) != 0 {
return errors.New("eckex: trailing data in key exchange")
}
hashed := sha256.Sum256(skey.Public)
if !ecdsa.Verify(signer, hashed[:], skey.R, skey.S) {
return errors.New("eckex: verification failure")
}
pub, err := unpackECPKIX(skey.Public)
if err != nil {
return err
}
priv, err := x509.ParseECPrivateKey(kex.priv)
util.Zero(kex.priv)
if err != nil {
return err
}
kex.shared, err = nistecdh.ECDH(priv, pub)
kex.shared = kex.shared[:32]
return err
}
开发者ID:jmptrader,项目名称:gocrypto,代码行数:33,代码来源:eckex.go
示例6: VerifyKeyPair
// Verify the secret key's range and if a test message signed with it verifies OK
// Returns nil if averything looks OK
func VerifyKeyPair(priv []byte, publ []byte) error {
const TestMessage = "Just some test message..."
hash := Sha2Sum([]byte(TestMessage))
pub_key, e := NewPublicKey(publ)
if e != nil {
return e
}
var key ecdsa.PrivateKey
key.D = new(big.Int).SetBytes(priv)
key.PublicKey = pub_key.PublicKey
if key.D.Cmp(big.NewInt(0)) == 0 {
return errors.New("pubkey value is zero")
}
if key.D.Cmp(secp256k1.N) != -1 {
return errors.New("pubkey value is too big")
}
r, s, err := ecdsa.Sign(rand.Reader, &key, hash[:])
if err != nil {
return errors.New("ecdsa.Sign failed: " + err.Error())
}
ok := ecdsa.Verify(&key.PublicKey, hash[:], r, s)
if !ok {
return errors.New("ecdsa.Sign Verify")
}
return nil
}
开发者ID:ripplecripple,项目名称:gocoin,代码行数:34,代码来源:wallet.go
示例7: VerifyBytes
// VerifyBytes creates a signature for buf, comparing it against the raw sig.
// If the sig is invalid, then ErrInvalidSignature is returned.
func (es *eccSigner) VerifyBytes(buf, sig []byte) error {
var err error
// check es.pub
if es.pub == nil {
return errors.New("eccSigner.VerifyBytes: pub cannot be nil")
}
// hash
h := es.hash.New()
_, err = h.Write(buf)
if err != nil {
return err
}
// check decoded length
if len(sig) != 2*es.keyLen {
return ErrInvalidSignature
}
r := big.NewInt(0).SetBytes(sig[:es.keyLen])
s := big.NewInt(0).SetBytes(sig[es.keyLen:])
// verify
if !ecdsa.Verify(es.pub, h.Sum(nil), r, s) {
return ErrInvalidSignature
}
return nil
}
开发者ID:knq,项目名称:jwt,代码行数:32,代码来源:ecc.go
示例8: TestSign
// TestSign - Return signatures for a signed message
func TestSign(w http.ResponseWriter, r *http.Request) {
conf := ConfLoad()
// Returns a Public / Private Key Pair
// Read JSON config from app.yaml
if v := os.Getenv("PRIV_KEY"); v != "" {
err := json.Unmarshal([]byte(v), &conf)
if err != nil {
fmt.Printf("%#v", conf)
panic(err)
}
}
// Get the public key
var pubkey ecdsa.PublicKey
pubkey = conf.PublicKey
// Try signing a message
message := []byte("99999999")
sig1, sig2, err := ecdsa.Sign(rand.Reader, &conf.PrivateKey, message)
if err != nil {
panic(err)
}
// Try verifying the signature
result := ecdsa.Verify(&pubkey, message, sig1, sig2)
if result != true {
panic("Unable to verify signature")
}
fmt.Fprintf(w, "message: %#v\n\nsig1: %#v\nsig2: %#v", string(message[:]), sig1, sig2)
}
开发者ID:capnfuzz,项目名称:qrtickets,代码行数:34,代码来源:web_handlers.go
示例9: verify
func verify(pub_key_filename string, msg []byte, signature []byte) bool {
// load ecdsa public key from file
pub_data, err := ioutil.ReadFile(pub_key_filename)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
ecpub, err := cyhcrypto.LoadECPublicKey(pub_data)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// verify signature
v_msg_hasher := sha256.New()
_, _ = v_msg_hasher.Write(msg)
v_hashed := v_msg_hasher.Sum(nil)
sig_r, sig_s, err := cyhcrypto.DecodeSignature(signature)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
result := ecdsa.Verify(ecpub, v_hashed, sig_r, sig_s)
return result
}
开发者ID:closeyourhead,项目名称:learn-golang,代码行数:27,代码来源:sample.go
示例10: VerifyHash
// verify a signature given the hash
func (v *ECDSAVerifier) VerifyHash(h, sig []byte) (err error) {
r, s := elliptic.Unmarshal(v.c, sig)
if r == nil || s == nil || !ecdsa.Verify(v.k, h, r, s) {
err = ErrInvalidSignature
}
return
}
开发者ID:majestrate,项目名称:go-i2p,代码行数:8,代码来源:ecdsa.go
示例11: Verify
func (key *ecdsaPublicKey) Verify(data []byte, sig *Signature) error {
if sig.Format != key.Type() {
return fmt.Errorf("ssh: signature type %s for key type %s", sig.Format, key.Type())
}
h := ecHash(key.Curve).New()
h.Write(data)
digest := h.Sum(nil)
// Per RFC 5656, section 3.1.2,
// The ecdsa_signature_blob value has the following specific encoding:
// mpint r
// mpint s
var ecSig struct {
R *big.Int
S *big.Int
}
if err := Unmarshal(sig.Blob, &ecSig); err != nil {
return err
}
if ecdsa.Verify((*ecdsa.PublicKey)(key), digest, ecSig.R, ecSig.S) {
return nil
}
return errors.New("ssh: signature did not verify")
}
开发者ID:backkom,项目名称:leanote-all,代码行数:27,代码来源:keys.go
示例12: CreateCertificate
// CreateCertificate requests the creation of a new enrollment certificate by the TLSCA.
//
func (tlscap *TLSCAP) CreateCertificate(ctx context.Context, in *pb.TLSCertCreateReq) (*pb.TLSCertCreateResp, error) {
Trace.Println("grpc TLSCAP:CreateCertificate")
id := in.Id.Id
sig := in.Sig
in.Sig = nil
r, s := big.NewInt(0), big.NewInt(0)
r.UnmarshalText(sig.R)
s.UnmarshalText(sig.S)
raw := in.Pub.Key
if in.Pub.Type != pb.CryptoType_ECDSA {
return nil, errors.New("unsupported key type")
}
pub, err := x509.ParsePKIXPublicKey(in.Pub.Key)
if err != nil {
return nil, err
}
hash := utils.NewHash()
raw, _ = proto.Marshal(in)
hash.Write(raw)
if ecdsa.Verify(pub.(*ecdsa.PublicKey), hash.Sum(nil), r, s) == false {
return nil, errors.New("signature does not verify")
}
if raw, err = tlscap.tlsca.createCertificate(id, pub.(*ecdsa.PublicKey), x509.KeyUsageDigitalSignature, in.Ts.Seconds, nil); err != nil {
Error.Println(err)
return nil, err
}
return &pb.TLSCertCreateResp{&pb.Cert{raw}, &pb.Cert{tlscap.tlsca.raw}}, nil
}
开发者ID:yhjohn163,项目名称:obc-peer,代码行数:37,代码来源:tlsca.go
示例13: MyVerify
func MyVerify(sign *Signature, pub *ecdsa.PublicKey, hashed []byte) error {
if !ecdsa.Verify(pub, hashed, (&big.Int{}).SetBytes(sign.R), (&big.Int{}).SetBytes(sign.S)) {
return errors.New("Verify error")
}
return nil
}
开发者ID:papercompute,项目名称:mychain,代码行数:7,代码来源:cr.go
示例14: Verify
// Verify verifyies the signature of the data in the io.Reader using this
// PublicKey. The alg parameter should identify the digital signature
// algorithm which was used to produce the signature and should be supported
// by this public key. Returns a nil error if the signature is valid.
func (k *ecPublicKey) Verify(data io.Reader, alg string, signature []byte) error {
// For EC keys there is only one supported signature algorithm depending
// on the curve parameters.
if k.signatureAlgorithm.HeaderParam() != alg {
return fmt.Errorf("unable to verify signature: EC Public Key with curve %q does not support signature algorithm %q", k.curveName, alg)
}
// signature is the concatenation of (r, s), base64Url encoded.
sigLength := len(signature)
expectedOctetLength := 2 * ((k.Params().BitSize + 7) >> 3)
if sigLength != expectedOctetLength {
return fmt.Errorf("signature length is %d octets long, should be %d", sigLength, expectedOctetLength)
}
rBytes, sBytes := signature[:sigLength/2], signature[sigLength/2:]
r := new(big.Int).SetBytes(rBytes)
s := new(big.Int).SetBytes(sBytes)
hasher := k.signatureAlgorithm.HashID().New()
_, err := io.Copy(hasher, data)
if err != nil {
return fmt.Errorf("error reading data to sign: %s", err)
}
hash := hasher.Sum(nil)
if !ecdsa.Verify(k.PublicKey, hash, r, s) {
return errors.New("invalid signature")
}
return nil
}
开发者ID:ericcapricorn,项目名称:deis,代码行数:35,代码来源:ec_key.go
示例15: ReadCertificate
// ReadCertificate reads a transaction certificate from the TCA.
//
func (tcap *TCAP) ReadCertificate(ctx context.Context, req *pb.TCertReadReq) (*pb.Cert, error) {
Trace.Println("grpc TCAP:ReadCertificate")
id := req.Id.Id
raw, err := tcap.tca.eca.readCertificate(id, x509.KeyUsageDigitalSignature)
if err != nil {
return nil, err
}
cert, err := x509.ParseCertificate(raw)
if err != nil {
return nil, err
}
sig := req.Sig
req.Sig = nil
r, s := big.NewInt(0), big.NewInt(0)
r.UnmarshalText(sig.R)
s.UnmarshalText(sig.S)
hash := sha3.New384()
raw, _ = proto.Marshal(req)
hash.Write(raw)
if ecdsa.Verify(cert.PublicKey.(*ecdsa.PublicKey), hash.Sum(nil), r, s) == false {
return nil, errors.New("signature does not verify")
}
raw, err = tcap.tca.readCertificate1(id, req.Ts.Seconds)
if err != nil {
return nil, err
}
return &pb.Cert{raw}, nil
}
开发者ID:tenc,项目名称:obc-peer-pre-public,代码行数:36,代码来源:tca.go
示例16: Verify
// Verify computes the hash of data and compares it to the signature using the
// given public key. Returns nil if the signature is correct.
func Verify(pubKeyPEM []byte, signature []byte, data io.Reader) error {
// Parse the public key
key, err := loadPublicKey(pubKeyPEM)
if err != nil {
return err
}
// Parse the signature
block, _ := pem.Decode(signature)
r, s, err := unmarshalSignature(block.Bytes)
if err != nil {
return err
}
// Compute the hash of the data
hash, err := hashReader(data)
if err != nil {
return err
}
// Verify the signature
if !ecdsa.Verify(key, hash, r, s) {
return errors.New("incorrect signature")
}
return nil
}
开发者ID:burkemw3,项目名称:syncthing,代码行数:29,代码来源:signature.go
示例17: verifyOcSig
func verifyOcSig(reqHash []byte, ocID msg.OcID, sig string) bool {
ocCredReader := strings.NewReader(ocID.String())
var x, y, r, s big.Int
n, err := fmt.Fscanf(ocCredReader, string(OC_ID_PREFIX)+"%x,%x", &x, &y)
if err != nil {
return false
}
n, err = ocCredReader.Read(make([]byte, 1))
if n != 0 || err != io.EOF {
return false
}
sigReader := strings.NewReader(sig)
n, err = fmt.Fscanf(sigReader, "%x,%x", &r, &s)
if err != nil {
return false
}
n, err = sigReader.Read(make([]byte, 1))
if n != 0 || err != io.EOF {
return false
}
curve := elliptic.P256()
pub := ecdsa.PublicKey{
Curve: curve,
X: &x,
Y: &y,
}
return ecdsa.Verify(&pub, reqHash, &r, &s)
}
开发者ID:therob3000,项目名称:decloud,代码行数:30,代码来源:cred.go
示例18: TestECDSASign
func TestECDSASign(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
conn, err := c.Dial(serverAddr)
if err != nil {
t.Fatal(err)
}
defer conn.Close()
sig, err := ecdsaKey.Sign(r, msg, h)
if err != nil {
t.Fatal(err)
}
if ecdsaPub, ok := ecdsaKey.Public().(*ecdsa.PublicKey); ok {
ecdsaSig := new(struct{ R, S *big.Int })
asn1.Unmarshal(sig, ecdsaSig)
if !ecdsa.Verify(ecdsaPub, msg, ecdsaSig.R, ecdsaSig.S) {
t.Log("ecdsa verify failed")
}
} else {
t.Fatal("couldn't use public key as ECDSA key")
}
}
开发者ID:Bren2010,项目名称:gokeyless,代码行数:26,代码来源:client_test.go
示例19: VerifySignature
// VerifySignature verifies that the passed in signature over data was created by the given PublicKey.
func VerifySignature(pubKey crypto.PublicKey, data []byte, sig DigitallySigned) error {
hash, hashType, err := generateHash(sig.Algorithm.Hash, data)
if err != nil {
return err
}
switch sig.Algorithm.Signature {
case RSA:
rsaKey, ok := pubKey.(*rsa.PublicKey)
if !ok {
return fmt.Errorf("cannot verify RSA signature with %T key", pubKey)
}
if err := rsa.VerifyPKCS1v15(rsaKey, hashType, hash, sig.Signature); err != nil {
return fmt.Errorf("failed to verify rsa signature: %v", err)
}
case DSA:
dsaKey, ok := pubKey.(*dsa.PublicKey)
if !ok {
return fmt.Errorf("cannot verify DSA signature with %T key", pubKey)
}
var dsaSig dsaSig
rest, err := asn1.Unmarshal(sig.Signature, &dsaSig)
if err != nil {
return fmt.Errorf("failed to unmarshal DSA signature: %v", err)
}
if len(rest) != 0 {
log.Printf("Garbage following signature %v", rest)
}
if dsaSig.R.Sign() <= 0 || dsaSig.S.Sign() <= 0 {
return errors.New("DSA signature contained zero or negative values")
}
if !dsa.Verify(dsaKey, hash, dsaSig.R, dsaSig.S) {
return errors.New("failed to verify DSA signature")
}
case ECDSA:
ecdsaKey, ok := pubKey.(*ecdsa.PublicKey)
if !ok {
return fmt.Errorf("cannot verify ECDSA signature with %T key", pubKey)
}
var ecdsaSig dsaSig
rest, err := asn1.Unmarshal(sig.Signature, &ecdsaSig)
if err != nil {
return fmt.Errorf("failed to unmarshal ECDSA signature: %v", err)
}
if len(rest) != 0 {
log.Printf("Garbage following signature %v", rest)
}
if ecdsaSig.R.Sign() <= 0 || ecdsaSig.S.Sign() <= 0 {
return errors.New("ECDSA signature contained zero or negative values")
}
if !ecdsa.Verify(ecdsaKey, hash, ecdsaSig.R, ecdsaSig.S) {
return errors.New("failed to verify ECDSA signature")
}
default:
return fmt.Errorf("unsupported Algorithm.Signature in signature: %v", sig.Algorithm.Hash)
}
return nil
}
开发者ID:jfrazelle,项目名称:cfssl,代码行数:60,代码来源:signature.go
示例20: Verify
// Verify checks a raw ECDSA signature.
// Returns true if it's valid and false if not.
func Verify(data []byte, sig *ECDSASignature, pubkey *ecdsa.PublicKey) bool {
// hash message
h := ecdsaHash.New()
h.Write(data)
digest := h.Sum(nil)
return ecdsa.Verify(pubkey, digest, sig.R, sig.S)
}
开发者ID:ericchiang,项目名称:cryptopasta,代码行数:10,代码来源:sign.go
注:本文中的crypto/ecdsa.Verify函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论