-
Notifications
You must be signed in to change notification settings - Fork 2
/
sign.go
78 lines (65 loc) · 1.73 KB
/
sign.go
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// vault sign is an example of how to use the sign/verify methods
package main
import (
"context"
"encoding/base64"
"fmt"
"log"
"os"
"time"
"github.com/pangeacyber/pangea-go/pangea-sdk/v4/pangea"
"github.com/pangeacyber/pangea-go/pangea-sdk/v4/service/vault"
)
func main() {
token := os.Getenv("PANGEA_VAULT_TOKEN")
if token == "" {
log.Fatal("Unauthorized: No token present")
}
vaultcli := vault.New(&pangea.Config{
Token: token,
Domain: os.Getenv("PANGEA_DOMAIN"),
})
ctx := context.Background()
fmt.Println("Generate key...")
name := "Go sign example " + time.Now().Format(time.RFC3339)
generateInput := &vault.AsymmetricGenerateRequest{
Algorithm: vault.AAed25519,
Purpose: vault.KPsigning,
CommonGenerateRequest: vault.CommonGenerateRequest{
Name: name,
},
}
generateResponse, err := vaultcli.AsymmetricGenerate(ctx, generateInput)
if err != nil {
log.Fatal(err)
}
fmt.Println(pangea.Stringify(generateResponse.Result))
fmt.Println("sign...")
message := "messagetosign"
data := base64.StdEncoding.EncodeToString([]byte(message))
signInput := &vault.SignRequest{
ID: generateResponse.Result.ID,
Message: data,
}
signResponse, err := vaultcli.Sign(ctx, signInput)
if err != nil {
log.Fatal(err)
}
fmt.Println(pangea.Stringify(signResponse.Result))
fmt.Println("Verify...")
verifyInput := &vault.VerifyRequest{
ID: generateResponse.Result.ID,
Message: data,
Signature: signResponse.Result.Signature,
}
verifyResponse, err := vaultcli.Verify(ctx, verifyInput)
if err != nil {
log.Fatal(err)
}
fmt.Println(pangea.Stringify(verifyResponse.Result))
if verifyResponse.Result.ValidSignature {
fmt.Println("Verify success")
} else {
fmt.Println("Verify failed")
}
}