* Refactor library, remove duplicates * Rebuild structure to use Messages and Signature models * Use PGPSplitMessage * Remove signature model * Various fixes * Add helpers with tests * Fixes, add some docs, add tests * Add attachment helpers * Add helpers Symmetric encryption * Edit docs + examples * Rename kr to keyRing * Various fixes for documentation * Edit JSON handling functions, add decrypt keyring via token * Add proposal changes doc * Fix CI * Drop *Message functions, join CleartextMessage and BinaryMessage * Change canonicalization and trimming only to text signatures * Add cleartextsignature, detach signature from message model, move helpers * Documentation, remove optional parameters * Move verification to separate model * Don't return message in VerifyDetached * Update table of contents in readme * Appease golint * Run go fmt * Rename Encrypt/DecryptMessageWithPassword to ..WithToken These functions shouldn't be used with user-provided passwords, as they don't do any key-stretching. * Change key generation usernames
56 lines
1.6 KiB
Go
56 lines
1.6 KiB
Go
package crypto
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// const testAttachmentEncrypted =
|
|
// `0ksB0fHC6Duezx/0TqpK/82HSl8+qCY0c2BCuyrSFoj6Dubd93T3//32jVYa624NYvfvxX+UxFKYKJxG09gFsU1IVc87cWvUgmUmgjU=`
|
|
|
|
func TestAttachmentGetKey(t *testing.T) {
|
|
testKeyPacketsDecoded, err := base64.StdEncoding.DecodeString(readTestFile("attachment_keypacket", false))
|
|
if err != nil {
|
|
t.Fatal("Expected no error while decoding base64 KeyPacket, got:", err)
|
|
}
|
|
|
|
symmetricKey, err := testPrivateKeyRing.DecryptSessionKey(testKeyPacketsDecoded)
|
|
if err != nil {
|
|
t.Fatal("Expected no error while decrypting KeyPacket, got:", err)
|
|
}
|
|
|
|
assert.Exactly(t, testSymmetricKey, symmetricKey)
|
|
}
|
|
|
|
func TestAttachmentSetKey(t *testing.T) {
|
|
keyPackets, err := testPublicKeyRing.EncryptSessionKey(testSymmetricKey)
|
|
if err != nil {
|
|
t.Fatal("Expected no error while encrypting attachment key, got:", err)
|
|
}
|
|
|
|
symmetricKey, err := testPrivateKeyRing.DecryptSessionKey(keyPackets)
|
|
if err != nil {
|
|
t.Fatal("Expected no error while decrypting attachment key, got:", err)
|
|
}
|
|
|
|
assert.Exactly(t, testSymmetricKey, symmetricKey)
|
|
}
|
|
|
|
func TestAttachnentEncryptDecrypt(t *testing.T) {
|
|
var testAttachmentCleartext = "cc,\ndille."
|
|
var message = NewPlainMessage([]byte(testAttachmentCleartext))
|
|
|
|
encSplit, err := testPrivateKeyRing.EncryptAttachment(message, "s.txt")
|
|
if err != nil {
|
|
t.Fatal("Expected no error while encrypting attachment, got:", err)
|
|
}
|
|
|
|
redecData, err := testPrivateKeyRing.DecryptAttachment(encSplit)
|
|
if err != nil {
|
|
t.Fatal("Expected no error while decrypting attachment, got:", err)
|
|
}
|
|
|
|
assert.Exactly(t, message, redecData)
|
|
}
|