Example #1
0
func ExampleEncryptArmor62Seal() {

	var err error

	// Make a new Keyring, initialized to be empty
	keyring := basic.NewKeyring()

	// The test message
	msg := []byte("The Magic Words are Squeamish Ossifrage")

	// Make a secret key for the sender
	var sender saltpack.BoxSecretKey
	sender, err = keyring.GenerateBoxKey()
	if err != nil {
		return
	}

	// And one for the receiver
	var receiver saltpack.BoxSecretKey
	receiver, err = keyring.GenerateBoxKey()
	if err != nil {
		return
	}

	// AllReceivers can contain more receivers (like the sender)
	// but for now, just the one.
	var ciphertext string
	allReceivers := []saltpack.BoxPublicKey{receiver.GetPublicKey()}
	ciphertext, err = saltpack.EncryptArmor62Seal(msg, sender, allReceivers, "")
	if err != nil {
		return
	}

	// The decrypted message should match the input mesasge.
	var msg2 []byte
	_, msg2, _, err = saltpack.Dearmor62DecryptOpen(ciphertext, keyring)
	if err != nil {
		return
	}

	fmt.Println(string(msg2))

	// Output:
	// The Magic Words are Squeamish Ossifrage
}
Example #2
0
func TestBasicBox(t *testing.T) {
	kr := NewKeyring()
	k1, err := kr.GenerateBoxKey()
	if err != nil {
		t.Fatal(err)
	}
	k2, err := kr.GenerateBoxKey()
	if err != nil {
		t.Fatal(err)
	}
	msg := randomMsg(t, 1024)
	text, err := saltpack.EncryptArmor62Seal(msg, k1, []saltpack.BoxPublicKey{k2.GetPublicKey()}, "")
	if err != nil {
		t.Fatal(err)
	}
	_, msg2, _, err := saltpack.Dearmor62DecryptOpen(text, kr)
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(msg, msg2) {
		t.Fatal("failed to recover message")
	}
}