Esempio n. 1
0
func TestSPKI(t *testing.T) {
	pk, sk, err := ed25519.GenerateKey(rand.Reader)
	if err != nil {
		panic(err)
	}

	outs, err := sx.SXCanonical.String(spki.FormEd25519PrivateKey(sk))
	if err != nil {
		panic(err)
	}

	v, err := sx.SX.Parse([]byte(outs))
	if err != nil {
		panic(fmt.Sprintf("failed to parse: %v: %#v", err, outs))
	}

	var sk2 [64]byte
	isPrivate, err := spki.LoadEd25519Key(v, &sk2)
	if err != nil {
		panic(fmt.Sprintf("failed to load: %v: %#v", err, v))
	}

	if !isPrivate {
		panic("should be private")
	}

	if !bytes.Equal(sk[:], sk2[:]) {
		panic("should be equal")
	}

	// public key

	outs, err = sx.SXCanonical.String(spki.FormEd25519PublicKeyFromPrivateKey(sk))
	if err != nil {
		panic(err)
	}

	v, err = sx.SX.Parse([]byte(outs))
	if err != nil {
		panic(err)
	}

	var sk3 [64]byte
	isPrivate, err = spki.LoadEd25519Key(v, &sk3)
	if err != nil {
		panic(err)
	}

	if isPrivate {
		panic("should be public")
	}

	if !bytes.Equal(pk[:], sk3[32:]) {
		panic("should be equal")
	}
}
Esempio n. 2
0
func doGenerateKeyEd25519() {
	_, sk, err := ed25519.GenerateKey(rand.Reader)
	log.Fatale(err, "cannot generate key")

	s, err := sx.SXCanonical.String(spki.FormEd25519PrivateKey(sk))
	log.Fatale(err, "cannot serialize key")

	fmt.Fprintf(os.Stderr, ";; Fingerprint: %v\n;;\n", hexFormat(sk[32:64]))

	fmt.Fprintf(os.Stderr, "%s", prefix(randomart.Generate(sk[32:64], " Ed25519").String()))

	w, err := armor.Encode(os.Stdout, "SPKI PRIVATE KEY", map[string]string{
		"Version": "Davka",
	})
	log.Fatale(err)

	defer w.Close()
	w.Write([]byte(s))
}