예제 #1
0
// 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)
	}
	if err := os.MkdirAll(filepath.Dir(secRing), 0700); err != nil {
		return "", err
	}
	f, err := wkfs.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
}
예제 #2
0
// DecryptMessage is largely from
// http://chiselapp.com/user/loser/repository/auricular/artifact/e14b57f441816f449105d201e7fb429f76907c65
func DecryptMessage(recipient, cipher string) (fromId uint64, msg string, err error) {
	// if DEBUG { log.Printf("Within decryptMessage: cipher == %v\n", cipher) }
	r := bytes.NewBufferString(cipher)
	// r := strings.NewReader(cipher)
	block, err := armor.Decode(r)
	if err != nil {
		return 0, "", fmt.Errorf("Error decrypting message: %v", err)
	}

	if DEBUG {
		log.Printf("Getting entity from private keyring\n")
	}
	myPrivateKey, err := GetEntityFrom(recipient, PRIVATE_KEYRING_FILENAME)
	if err != nil {
		return 0, "", fmt.Errorf("Error getting private key for %s: %v",
			recipient, err)
	}
	entities := openpgp.EntityList([]*openpgp.Entity{myPrivateKey})
	if DEBUG {
		log.Printf("Got entities\n")
	}
	details, err := openpgp.ReadMessage(block.Body, entities, nil, nil)
	if err != nil {
		return 0, "", fmt.Errorf("Error reading message block body: %v", err)
	}

	// Read the message body
	if DEBUG {
		log.Printf("Reading raw message body\n")
	}
	raw, err := ioutil.ReadAll(details.UnverifiedBody)
	if err != nil {
		return 0, "", fmt.Errorf("Error reading decrypted message body: %v",
			err)
	}
	return details.SignedByKeyId, string(raw), nil
}
예제 #3
0
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)
	}
}