-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcrypto.go
More file actions
66 lines (55 loc) · 2.05 KB
/
Copy pathcrypto.go
File metadata and controls
66 lines (55 loc) · 2.05 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package canarytail
import (
"crypto/ed25519"
"encoding/base64"
"math/rand"
"time"
"golang.org/x/crypto/curve25519"
)
// SignString signs a Canary given a private key
func SignString(formattedCanary string, privateKey ed25519.PrivateKey) []byte {
message := []byte(formattedCanary)
return ed25519.Sign(privateKey, message)
}
// ValidateSignatureString validates a Canary's signature given the corresponding public key
func ValidateSignatureString(formattedCanary string, signature []byte, publicKey ed25519.PublicKey) bool {
message := []byte(formattedCanary)
return ed25519.Verify(publicKey, message, signature)
}
// deprecated: curve25519 vs ed25519
func generateRandomPrivateKey() [32]byte {
rand.Seed(time.Now().UnixNano())
var privateKey [32]byte
for i := range privateKey[:] {
privateKey[i] = byte(rand.Intn(256))
}
return privateKey
}
// depracated: curve25519 vs ed25519
func generatePublicKey(privateKey [32]byte) [32]byte {
var publicKey [32]byte
curve25519.ScalarBaseMult(&publicKey, &privateKey)
return publicKey
}
// GenerateKeyPair generates an Ed25519 key pair for signatures. See https://ed25519.cr.yp.to/.
//
// These functions are also compatible with the “Ed25519” function defined in RFC 8032.
// However, unlike RFC 8032's formulation, this package's private key representation
// includes a public key suffix to make multiple signing operations with the same key more efficient.
// This package refers to the RFC 8032 private key as the “seed”.
//
func GenerateKeyPair() (ed25519.PublicKey, ed25519.PrivateKey, error) {
return ed25519.GenerateKey(nil)
}
// ParsePublicKey parses a public key in string form
func ParsePublicKey(publicKey string) (ed25519.PublicKey, error) {
return base64.StdEncoding.DecodeString(publicKey)
}
// ParsePrivateKey parses a private key in string form
func ParsePrivateKey(privateKey string) (ed25519.PrivateKey, error) {
return base64.StdEncoding.DecodeString(privateKey)
}
// FormatKey formats a key into a base64 string
func FormatKey(key []byte) string {
return base64.StdEncoding.EncodeToString(key)
}