Skip to content

Commit

Permalink
Update GenerateKeyPair to return string literals
Browse files Browse the repository at this point in the history
Based on great feedback from @gstro, normalize the `GenerateKeyPair()` helper function to return Base64URL-encoded strings (instead of raw bytes) so we're consistent between the various SDKs.
  • Loading branch information
Eric Mann committed Aug 28, 2017
1 parent 15e72ad commit 4ba1d4a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
18 changes: 11 additions & 7 deletions client_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,17 @@ func setup() {
if err != nil {
dieErr(err)
}
pubKey := ClientKey{Curve25519: base64Encode(pub[:])}
pubBytes, _ := base64Decode(pub)
privBytes, _ := base64Decode(priv)
pubKey := ClientKey{Curve25519: pub}

pub2, priv2, err := GenerateKeyPair()
if err != nil {
dieErr(err)
}
pubKey2 := ClientKey{Curve25519: base64Encode(pub2[:])}
pubBytes2, _ := base64Decode(pub2)
privBytes2, _ := base64Decode(priv2)
pubKey2 := ClientKey{Curve25519: pub2}

clientDetails, err := RegisterClient(token, clientName, pubKey, apiURL)
if err != nil {
Expand All @@ -72,8 +76,8 @@ func setup() {
ClientEmail: "",
APIKeyID: clientDetails.ApiKeyID,
APISecret: clientDetails.ApiSecret,
PublicKey: pub,
PrivateKey: priv,
PublicKey: makePublicKey(pubBytes),
PrivateKey: makePrivateKey(privBytes),
APIBaseURL: apiURL,
Logging: false,
}
Expand All @@ -93,8 +97,8 @@ func setup() {
ClientEmail: "",
APIKeyID: shareClientDetails.ApiKeyID,
APISecret: shareClientDetails.ApiSecret,
PublicKey: pub2,
PrivateKey: priv2,
PublicKey: makePublicKey(pubBytes2),
PrivateKey: makePrivateKey(privBytes2),
APIBaseURL: apiURL,
Logging: false,
}
Expand All @@ -121,7 +125,7 @@ func TestRegistration(t *testing.T) {
t.Fatal(err)
}

pubKey := ClientKey{Curve25519: base64Encode(pub[:])}
pubKey := ClientKey{Curve25519: pub}
clientName := "test-client-" + base64Encode(randomSecretKey()[:8])

client, err := RegisterClient(token, clientName, pubKey, apiURL)
Expand Down
9 changes: 6 additions & 3 deletions crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,20 @@ func secretBoxDecryptFromBase64(ciphertext, nonce string, key secretKey) ([]byte
const publicKeySize = 32
const privateKeySize = 32

// PublicKey is an alias of a 32-byte array representing the public key component
type PublicKey *[publicKeySize]byte

// PrivateKey is an alias of a 32-byte array representing the private key component
type PrivateKey *[privateKeySize]byte

// GenerateKeyPair creates a new Curve25519 keypair for cryptographic operations
func GenerateKeyPair() (PublicKey, PrivateKey, error) {
func GenerateKeyPair() (string, string, error) {
pub, priv, err := box.GenerateKey(rand.Reader)
if err != nil {
return nil, nil, err
return "", "", err
}

return pub, priv, nil
return encodePublicKey(pub), encodePrivateKey(priv), nil
}

func makePublicKey(b []byte) PublicKey {
Expand Down

0 comments on commit 4ba1d4a

Please sign in to comment.