Cipher.decrypt

struct Cipher
static
const(Document)
decrypt
(
const(SecureNet) net
,
const(CipherDocument) cipher_doc
)

Examples

1 import std.algorithm.searching : all, any;
2 import tagion.basic.Types : FileExtension;
3 import tagion.basic.basic : fileId;
4 import tagion.crypto.SecureNet;
5 import tagion.hibon.Document : Document;
6 import tagion.hibon.HiBON : HiBON;
7 import tagion.utils.Miscellaneous : decode;
8 
9 immutable passphrase = "Secret pass word";
10 auto net = new StdSecureNet; /// Only works with ECDSA for now 
11 net.generateKeyPair(passphrase);
12 
13 immutable some_secret_message = "Text to be encrypted by ECC public key and " ~
14     "decrypted by its corresponding ECC private key";
15 auto hibon = new HiBON;
16 hibon["text"] = some_secret_message;
17 const secret_doc = Document(hibon);
18 
19 { // Encrypt and Decrypt secret message
20     auto dummy_net = new StdSecureNet;
21     auto secret_cipher_doc = Cipher.encrypt(dummy_net, net.pubkey, secret_doc).serialize;
22     const encrypted_doc = Cipher.decrypt(net, CipherDocument(Document(secret_cipher_doc)));
23     assert(encrypted_doc["text"].get!string == some_secret_message);
24     assert(secret_doc.data == encrypted_doc.data);
25 }
26 
27 { // Use of the wrong privat-key
28     auto dummy_net = new StdSecureNet;
29     auto wrong_net = new StdSecureNet;
30     immutable wrong_passphrase = "wrong word";
31     wrong_net.generateKeyPair(wrong_passphrase);
32     bool cipher_decrypt_error;
33     bool cipher_decrypt_crc_error;
34     while (!cipher_decrypt_error || !cipher_decrypt_crc_error) {
35         const secret_cipher_doc = Cipher.encrypt(dummy_net, wrong_net.pubkey, secret_doc);
36         try {
37             const encrypted_doc = Cipher.decrypt(net, secret_cipher_doc);
38             assert(secret_doc != encrypted_doc);
39             if (!encrypted_doc.empty) {
40                 break; /// Run the loop until the decrypt does not fail
41             }
42         }
43         catch (ConsensusException e) {
44             cipher_decrypt_error |= (e.code == ConsensusFailCode.CIPHER_DECRYPT_ERROR);
45             cipher_decrypt_crc_error |= (e.code == ConsensusFailCode.CIPHER_DECRYPT_CRC_ERROR);
46         }
47     }
48 }
49 
50 { // Encrypt and Decrypt secrte message with owner privat-key
51     const secret_cipher_doc = Cipher.encrypt(net, secret_doc);
52     const encrypted_doc = Cipher.decrypt(net, secret_cipher_doc);
53     assert(encrypted_doc["text"].get!string == some_secret_message);
54     assert(secret_doc.data == encrypted_doc.data);
55 }
56