forked from MetaMask/go-did-it
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.go
More file actions
44 lines (39 loc) · 1.75 KB
/
utilities.go
File metadata and controls
44 lines (39 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package did
import (
"fmt"
"github.com/MetaMask/go-did-it/crypto"
)
// TryAllVerifyBytes tries to verify the signature as bytes with all the methods in the slice.
// It returns true if the signature is verified, and the method that verified it.
// If no method verifies the signature, it returns false and nil.
func TryAllVerifyBytes(methods []VerificationMethodSignature, data []byte, sig []byte, opts ...crypto.SigningOption) (bool, VerificationMethodSignature) {
for _, method := range methods {
if valid, err := method.VerifyBytes(data, sig, opts...); err == nil && valid {
return true, method
}
}
return false, nil
}
// TryAllVerifyASN1 tries to verify the signature as ASN.1 with all the methods in the slice.
// It returns true if the signature is verified, and the method that verified it.
// If no method verifies the signature, it returns false and nil.
func TryAllVerifyASN1(methods []VerificationMethodSignature, data []byte, sig []byte, opts ...crypto.SigningOption) (bool, VerificationMethodSignature) {
for _, method := range methods {
if valid, err := method.VerifyASN1(data, sig, opts...); err == nil && valid {
return true, method
}
}
return false, nil
}
// FindMatchingKeyAgreement tries to find a matching key agreement method for the given private key type.
// It returns the shared key as well as the selected method.
// If no matching method is found, it returns an error.
func FindMatchingKeyAgreement(methods []VerificationMethodKeyAgreement, priv crypto.PrivateKeyKeyExchange) ([]byte, VerificationMethodKeyAgreement, error) {
for _, method := range methods {
if method.PrivateKeyIsCompatible(priv) {
key, err := method.KeyExchange(priv)
return key, method, err
}
}
return nil, nil, fmt.Errorf("no matching key agreement found")
}