1 /******************************************************************************* 2 3 libgcrypt with algorithm Twofish and mode CFB 4 5 Requires linking with libgcrypt: 6 -L-lgcrypt 7 8 See_Also: 9 https://en.wikipedia.org/wiki/Twofish 10 https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation 11 12 Copyright: 13 Copyright (c) 2009-2016 dunnhumby Germany GmbH. 14 All rights reserved. 15 16 License: 17 Boost Software License Version 1.0. See LICENSE_BOOST.txt for details. 18 Alternatively, this file may be distributed under the terms of the Tango 19 3-Clause BSD License (see LICENSE_BSD.txt for details). 20 21 Bear in mind this module provides bindings to an external library that 22 has its own license, which might be more restrictive. Please check the 23 external library license to see which conditions apply for linking. 24 25 *******************************************************************************/ 26 27 module ocean.util.cipher.gcrypt.Twofish; 28 29 import ocean.util.cipher.gcrypt.core.Gcrypt; 30 import ocean.transition; 31 32 33 /******************************************************************************* 34 35 Gcrypt with Twofish with mode CFB. 36 37 See usage example in unittest below. 38 39 *******************************************************************************/ 40 41 public alias GcryptWithIV!(Algorithm.GCRY_CIPHER_TWOFISH, Mode.GCRY_CIPHER_MODE_CFB) Twofish; 42 43 version ( UnitTest ) 44 { 45 import ocean.core.Test; 46 } 47 48 /// Usage example 49 unittest 50 { 51 // Twofish requires a key of length 32 bytes 52 auto key = cast(Immut!(ubyte)[])"a key of 32 bytesa key of32bytes"; 53 // Twofish requires an initialisation vector of length 16 bytes. 54 auto iv = cast(Immut!(ubyte)[])"a iv of 16 bytes"; 55 56 istring text = "This is a text we are going to encrypt"; 57 mstring encrypted_text, decrypted_text; 58 59 // Create the class 60 auto two = new Twofish(key); 61 62 // encryption/decryption is done in place so first copy the plain text to a 63 // buffer. 64 encrypted_text ~= text; 65 66 // The actual encryption. 67 two.encrypt(encrypted_text, iv); 68 69 // Since decryption is done in place we copy the decrypted string to a new 70 // buffer. 71 decrypted_text ~= encrypted_text; 72 73 // The decryption call 74 two.decrypt(decrypted_text, iv); 75 76 // We have now successfully encrypted and decrypted a string. 77 test!("==")(text, decrypted_text); 78 } 79 80 81 // Test that only keys of the provided length are allowed 82 unittest 83 { 84 auto key = cast(Immut!(ubyte)[])"a key of 32 bytesa key of32bytes"; 85 Twofish.testFixedKeyLength(key); 86 }