108 lines
2.8 KiB
Go
108 lines
2.8 KiB
Go
package shared
|
|
|
|
import (
|
|
"crypto"
|
|
"crypto/ed25519"
|
|
"crypto/rsa"
|
|
"crypto/sha256"
|
|
"crypto/x509"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
const testStringToSign = "some random text that I made up at work and definitely has no other purpose"
|
|
|
|
func TestGenerateKeypairRSA(t *testing.T) {
|
|
publicBytes, privateBytes, err := GenerateKeypair(false)
|
|
if err != nil {
|
|
t.Fatalf("generation failed: %v", err)
|
|
}
|
|
publicUntyped, err := x509.ParsePKIXPublicKey(publicBytes)
|
|
if err != nil {
|
|
t.Fatalf("parsing public bytes into key failed: %v", err)
|
|
}
|
|
public, ok := publicUntyped.(*rsa.PublicKey)
|
|
if !ok {
|
|
t.Fatal("asserting public key as rsa.PublicKey failed")
|
|
}
|
|
private, err := x509.ParsePKCS1PrivateKey(privateBytes)
|
|
if err != nil {
|
|
t.Fatalf("parsing private bytes into key failed: %v", err)
|
|
}
|
|
if err := private.Validate(); err != nil {
|
|
t.Fatalf("validation of private key failed: %v", err)
|
|
}
|
|
genPublicRaw := private.Public()
|
|
genPublic, ok := genPublicRaw.(*rsa.PublicKey)
|
|
if !reflect.DeepEqual(*public, *genPublic) {
|
|
t.Fatal("public from generator and from private are different")
|
|
}
|
|
}
|
|
|
|
func TestGenerateKeypairED(t *testing.T) {
|
|
publicBytes, privateBytes, err := GenerateKeypair(true)
|
|
if err != nil {
|
|
t.Fatalf("generation failed: %v", err)
|
|
}
|
|
publicUntyped, err := x509.ParsePKIXPublicKey(publicBytes)
|
|
if err != nil {
|
|
t.Fatalf("parsing public bytes into key failed: %v", err)
|
|
}
|
|
public, ok := publicUntyped.(ed25519.PublicKey)
|
|
if !ok {
|
|
t.Fatal("asserting public key as ed25519.PublicKey failed")
|
|
}
|
|
private := ed25519.PrivateKey(privateBytes)
|
|
pubGenRaw := private.Public()
|
|
pubGen, ok := pubGenRaw.(ed25519.PublicKey)
|
|
if !ok {
|
|
t.Fatal("public key generated by private key is not of type ed25519.PublicKey")
|
|
}
|
|
if !reflect.DeepEqual(pubGen, public) {
|
|
t.Fatal("public from generator and from private are different")
|
|
}
|
|
}
|
|
|
|
func TestSignRSA(t *testing.T) {
|
|
publicBytes, privateBytes, err := GenerateKeypair(false)
|
|
if err != nil {
|
|
t.Fatalf("failed to generate RSA keypair: %v", err)
|
|
}
|
|
pubRaw, err := x509.ParsePKIXPublicKey(publicBytes)
|
|
if err != nil {
|
|
t.Fatalf("failed to parse public rsa key: %v", err)
|
|
}
|
|
pub, ok := pubRaw.(*rsa.PublicKey)
|
|
if !ok {
|
|
t.Fatal("parsed public key is not *rsa.PublicKey")
|
|
}
|
|
hash := sha256.Sum256([]byte(testStringToSign))
|
|
signed, err := Sign(testStringToSign, privateBytes, true)
|
|
if err != nil {
|
|
t.Fatalf("failed to sign test string: %v", err)
|
|
}
|
|
|
|
if rsa.VerifyPKCS1v15(pub, crypto.SHA256, hash[:], signed) != nil {
|
|
t.Fatal("failed to verify")
|
|
}
|
|
}
|
|
|
|
func TestKeyBytesToPem(t *testing.T) {
|
|
type args struct {
|
|
bytes []byte
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want string
|
|
}{
|
|
// TODO: Add test cases.
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := KeyBytesToPem(tt.args.bytes, true); got != tt.want {
|
|
t.Errorf("KeyBytesToPem() = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|