103 lines
2.8 KiB
Go
103 lines
2.8 KiB
Go
package crypto_test
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"testing"
|
|
|
|
"git.clan.lol/clan/data-mesher/pkg/crypto"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestCrypto_ReadFile(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
as := require.New(t)
|
|
|
|
// generate a private key with `openssl genpkey -algorithm ed25519 -out private.pem`
|
|
// generate the public key with `openssl pkey -in private.pem -pubout
|
|
pubKey, err := crypto.ReadPublicKey("./key_public.pem")
|
|
as.NoError(err)
|
|
|
|
as.Equal("UppdEDP5k/9OgrKzGA1Q5ph9kPoIrNRd5y3w5IW+ZSs=", pubKey.String())
|
|
|
|
// parse without the enclosing PEM block
|
|
pubKey, err = crypto.ParsePublicKey([]byte("MCowBQYDK2VwAyEAUppdEDP5k/9OgrKzGA1Q5ph9kPoIrNRd5y3w5IW+ZSs="))
|
|
as.NoError(err)
|
|
|
|
as.Equal("UppdEDP5k/9OgrKzGA1Q5ph9kPoIrNRd5y3w5IW+ZSs=", pubKey.String())
|
|
|
|
// parse without any prefix indicating the key type, just the straight key encoded as base64
|
|
pubKey, err = crypto.ParsePublicKey([]byte("UppdEDP5k/9OgrKzGA1Q5ph9kPoIrNRd5y3w5IW+ZSs="))
|
|
as.NoError(err)
|
|
|
|
as.Equal("UppdEDP5k/9OgrKzGA1Q5ph9kPoIrNRd5y3w5IW+ZSs=", pubKey.String())
|
|
|
|
// read private key from file
|
|
privKey, err := crypto.ReadPrivateKey("./key_private.pem")
|
|
as.NoError(err)
|
|
|
|
// string value of the private key should be the public key
|
|
// we don't want to accidentally leak secrets
|
|
as.Equal("UppdEDP5k/9OgrKzGA1Q5ph9kPoIrNRd5y3w5IW+ZSs=", privKey.String())
|
|
|
|
// parse without the enclosing PEM block
|
|
privKey, err = crypto.ParsePrivateKey([]byte("MC4CAQAwBQYDK2VwBCIEIK1UTStQuhD/FzAVEtzASlHJ7nuqnlKLClPy29qikd/q"))
|
|
as.NoError(err)
|
|
|
|
as.Equal("UppdEDP5k/9OgrKzGA1Q5ph9kPoIrNRd5y3w5IW+ZSs=", privKey.String())
|
|
|
|
as.True(privKey.Public.Equal(pubKey))
|
|
}
|
|
|
|
func TestPrivateKey_PEM(t *testing.T) {
|
|
t.Parallel()
|
|
as := require.New(t)
|
|
|
|
// generate a key
|
|
key, err := crypto.GenerateKey(rand.Reader)
|
|
as.NoError(err)
|
|
|
|
// get PEM representation
|
|
pemData, err := key.PEM()
|
|
as.NoError(err)
|
|
as.NotEmpty(pemData)
|
|
|
|
// verify PEM format
|
|
pemStr := string(pemData)
|
|
as.Contains(pemStr, "-----BEGIN PRIVATE KEY-----")
|
|
as.Contains(pemStr, "-----END PRIVATE KEY-----")
|
|
|
|
// parse back from PEM
|
|
parsed, err := crypto.ParsePrivateKey(pemData)
|
|
as.NoError(err)
|
|
|
|
// should be identical (compare via public key since private key doesn't expose raw bytes)
|
|
as.True(parsed.Public.Equal(key.Public))
|
|
}
|
|
|
|
func TestPublicKey_PEM(t *testing.T) {
|
|
t.Parallel()
|
|
as := require.New(t)
|
|
|
|
// generate a key
|
|
key, err := crypto.GenerateKey(rand.Reader)
|
|
as.NoError(err)
|
|
|
|
// get PEM representation
|
|
pemData, err := key.Public.PEM()
|
|
as.NoError(err)
|
|
as.NotEmpty(pemData)
|
|
|
|
// verify PEM format
|
|
pemStr := string(pemData)
|
|
as.Contains(pemStr, "-----BEGIN PUBLIC KEY-----")
|
|
as.Contains(pemStr, "-----END PUBLIC KEY-----")
|
|
|
|
// parse back from PEM
|
|
parsed, err := crypto.ParsePublicKey(pemData)
|
|
as.NoError(err)
|
|
|
|
// should be identical
|
|
as.True(parsed.Equal(key.Public))
|
|
}
|