func generateNewSecRing(filename string) (keyId string, err error) { ent, err := jsonsign.NewEntity() if err != nil { return "", fmt.Errorf("generating new identity: %v", err) } f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600) if err != nil { return "", err } defer f.Close() err = jsonsign.WriteKeyRing(f, openpgp.EntityList([]*openpgp.Entity{ent})) if err != nil { return "", fmt.Errorf("writing new key ring to %s: %v", filename, err) } return ent.PrimaryKey.KeyIdShortString(), nil }
// GenerateNewSecRing creates a new secret ring file secRing, with // a new GPG identity. It returns the public keyId of that identity. // It returns an error if the file already exists. func GenerateNewSecRing(secRing string) (keyId string, err error) { ent, err := NewEntity() if err != nil { return "", fmt.Errorf("generating new identity: %v", err) } f, err := os.OpenFile(secRing, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600) if err != nil { return "", err } err = WriteKeyRing(f, openpgp.EntityList([]*openpgp.Entity{ent})) if err != nil { f.Close() return "", fmt.Errorf("Could not write new key ring to %s: %v", secRing, err) } if err := f.Close(); err != nil { return "", fmt.Errorf("Could not close %v: %v", secRing, err) } return ent.PrimaryKey.KeyIdShortString(), nil }
func GenerateNewSecRingStruct() (identity_secring *IdentitySecring, err error) { identity_secring = &IdentitySecring{} ent, err := NewEntity() if err != nil { return nil, fmt.Errorf("generating new identity: %v", err) } f := bytes.NewBuffer(identity_secring.Secring) err = WriteKeyRing(f, openpgp.EntityList([]*openpgp.Entity{ent})) if err != nil { //f.Close() return nil, fmt.Errorf("Could not write new key ring to byte stream in struct: %v", err) } // if err := f.Close(); err != nil { // return "", fmt.Errorf("Could not close %v: %v", secRing, err) // } err = nil identity_secring.KeyId = ent.PrimaryKey.KeyIdShortString() identity_secring.Secring = f.Bytes() return }
func TestWriteKeyRing(t *testing.T) { ent, err := EntityFromSecring("26F5ABDA", "testdata/test-secring.gpg") if err != nil { t.Fatalf("NewEntity: %v", err) } var buf bytes.Buffer err = WriteKeyRing(&buf, openpgp.EntityList([]*openpgp.Entity{ent})) if err != nil { t.Fatalf("WriteKeyRing: %v", err) } el, err := openpgp.ReadKeyRing(&buf) if err != nil { t.Fatalf("ReadKeyRing: %v", err) } if len(el) != 1 { t.Fatalf("ReadKeyRing read %d entities; want 1", len(el)) } orig := entityString(ent) got := entityString(el[0]) if orig != got { t.Fatalf("original vs. wrote-then-read entities differ:\norig: %s\n got: %s", orig, got) } }