Ejemplo n.º 1
0
func main() {
	store := pemutil.Store{}
	pemutil.PEM{"rsa-public.pem", "rsa-private.pem"}.Load(store)

	var ok bool
	var key *rsa.PrivateKey
	var pubKey *rsa.PublicKey

	if key, ok = store[pemutil.RSAPrivateKey].(*rsa.PrivateKey); !ok {
		log.Fatalln("key should be a *rsa.PrivateKey")
	}

	if pubKey, ok = store[pemutil.PublicKey].(*rsa.PublicKey); !ok {
		log.Fatalln("public key should be *rsa.PublicKey")
	}

	if !reflect.DeepEqual(pubKey, &key.PublicKey) {
		log.Fatalln("generated key and public key don't match")
	}

	// get pem data
	pemBuf, err := store.Bytes()
	if err != nil {
		log.Fatalln(err)
	}

	os.Stdout.Write(pemBuf)
}
Ejemplo n.º 2
0
func main() {
	var keyset pemutil.Store
	var err error

	flag.Parse()

Generate:
	switch *flagAlg {
	case "sym":
		if *flagKeyLen == 0 {
			err = errors.New("must specify key length (-l)")
			break Generate
		}
		keyset, err = pemutil.GenerateSymmetricKeySet(*flagKeyLen)

	case "rsa":
		if *flagKeyLen == 0 {
			err = errors.New("must specify key length (-l)")
			break Generate
		}
		keyset, err = pemutil.GenerateRSAKeySet(*flagKeyLen)

	case "ecc":
		var curve elliptic.Curve
		switch strings.ToUpper(*flagCurve) {
		case "P224":
			curve = elliptic.P224()
		case "P256":
			curve = elliptic.P256()
		case "P384":
			curve = elliptic.P384()
		case "P521":
			curve = elliptic.P521()

		default:
			err = errors.New("unknown curve")
			break Generate
		}

		keyset, err = pemutil.GenerateECKeySet(curve)

	default:
		err = errors.New("unknown key type")
	}

	// check for errors
	if err != nil {
		fmt.Fprintf(os.Stderr, "error: %v\n", err)
		flag.Usage()
		os.Exit(1)
	}

	// encode pem data
	buf, err := keyset.Bytes()
	if err != nil {
		fmt.Fprintf(os.Stderr, "error: %v\n", err)
		os.Exit(1)
	}

	// output
	_, err = os.Stdout.Write(buf)
	if err != nil {
		fmt.Fprintf(os.Stderr, "error: %v\n", err)
		os.Exit(1)
	}
}