Example #1
0
func NewPair(name, comment, email string) (*openpgp.Entity, error) {
	entity, err := openpgp.NewEntity(name, comment, email, nil)
	if err != nil {
		log.PrintError("authit.crypto.NewPair", err)
		return nil, err
	}

	// Sign all the identities
	for _, id := range entity.Identities {
		err := id.SelfSignature.SignUserId(id.UserId.Id, entity.PrimaryKey, entity.PrivateKey, nil)
		if err != nil {
			log.PrintError("authit.crypto.NewPair", err)
			return nil, err
		}
	}
	return entity, nil
}
Example #2
0
func Private(entity *openpgp.Entity) (string, error) {
	var output bytes.Buffer
	writeBuffer := bufio.NewWriter(&output)
	private, err := armor.Encode(writeBuffer, openpgp.PrivateKeyType, nil)
	if err != nil {
		log.PrintError("authit.crypto.Private", err)
		return "", err
	}

	entity.SerializePrivate(private, nil)
	private.Close()
	writeBuffer.Flush()
	return output.String(), nil
}
Example #3
0
File: gen.go Project: authit/crypto
func Gen() {
	pair, err := NewPair(
		viper.GetString("name"),
		viper.GetString("comment"),
		viper.GetString("email"),
	)
	if log.PrintError("crypto", err) != nil {
		return
	}
	public, err := Public(pair)
	if log.PrintError("crypto", err) != nil {
		return
	}
	private, err := Private(pair)
	if log.PrintError("crypto", err) != nil {
		return
	}
	if viper.GetBool("stdout") {
		fmt.Println(public)
		fmt.Println(private)
	}
	if viper.GetString("path") != "" {
		err := os.MkdirAll(viper.GetString("path"), 0700)
		if log.PrintError("crypto", err) != nil {
			return
		}
		err = ioutil.WriteFile(
			viper.GetString("path")+"public.pgp",
			[]byte(public),
			0600,
		)
		if log.PrintError("crypto", err) != nil {
			return
		}
		err = ioutil.WriteFile(
			viper.GetString("path")+"private.pgp",
			[]byte(private),
			0600,
		)
		if log.PrintError("crypto", err) != nil {
			return
		}
	}
}