[go: up one dir, main page]

cipher 0.1.1

feistel cipher implementation in rust. Converts str -> Vec<u32>
Documentation
#![feature(test)]
extern crate test;
extern crate cipher;
use cipher::feistel_encrypt;
use cipher::feistel_decrypt;
use test::Bencher;

#[bench]
fn sentence_encryption(b: &mut Bencher) {
    let plaintext = "In the election that followed, his widow, Sirimavo
        Bandaranaike, became prime minister on the sympathy vote.";
    b.iter(|| feistel_encrypt(plaintext, 19652000, 158));
}

#[bench]
fn sentence_decryption(b: &mut Bencher) {
    let ciphertext = vec![2571093993, 1150472712, 614563871, 3509912448,
    2949655941, 3780934990, 1869949444, 2665866390, 4237835233, 4195977159,
    1528281238, 3415170568, 1980798752, 3764872917, 1150472712, 4148994966,
    762872726, 18868128, 1326266952, 2001816942, 1869949444, 2388883055,
    3506462930, 1381068616, 1853370852, 1720565976, 1436985582, 1019426508,
    3460560700, 835343916, 2913789266, 831144132, 1542884151, 4058696829,
    2070600185, 2376192468, 2483744318, 4046802884, 3506462930, 2376192468,
    3010443061, 3537016722, 3379935085, 1537914793, 3858039052, 2483744318,
    748466817, 1209016909, 3901360541, 4279974290, 1145570795, 1788738485,
    1788738485, 1788738485, 1788738485, 1788738485, 1788738485, 1788738485,
    1788738485, 1788738485, 1788738485, 1788738485, 1788738485, 1788738485,
    1788738485, 4287230428, 3527595119, 512495351, 1293845163, 484345627,
    2053489555, 2835082591, 3536995766, 2521262901, 2739336475, 2304120468,
    2705308009 , 3031438619, 663514344, 1190468015, 2229352288, 2164990375,
    3692666785, 2872135153, 2835082591, 1937129042, 700501386, 4082882478,
    581739285, 2620649323, 2705308009, 3758882884, 4115154850, 153991973,
    3999316298, 913652860, 2056535692, 251073862, 3034865106, 1246442496,
    3042673105, 3180332087, 4278193680 , 2271627602, 1499269049, 489801195,
    3011516955, 760804792, 4115154850, 489801195, 1055938534, 1045746652,
    2904559349, 170049894, 3537942949, 3011516955, 1268481837, 3270737718,
    3202267887, 1320172744, 3837456752, 2286581121, 2286581121, 2286581121,
    2286581121, 2286581121, 2286581121, 2286581121, 2286581121, 2286581121,
    2286581121, 2286581121, 2286581121, 2286581121, 2286581121, 1825755713,
    1437005534, 1735211478, 1427051896, 3498287168];
    b.iter(|| {
        let cipher_clone = ciphertext.clone();
        feistel_decrypt(cipher_clone, 19652000, 158);
    });
}

#[bench]
fn paragraph_encryption(b: &mut Bencher) {
    let plaintext = "Before my departure I issued a joint statement with \
        Prime Minister Djuanda on trade and cultural matters. We had had \
        several talks since he received me at Jakarta aiport. He was an \
        excellent man—able, highly educated, realistic, and resigned to \
        the difficulties to his country. We had spoken for hours, \
        sometimes in Bahasa Indonesia. During one exchange over dinner, I \
        remarked that Indonesia was blessed with very fertile soild, a \
        favorable climate, and abundant resources. He looked at me sadly \
        and said, \"God is for us, but we are against ourselves.\" I felt \
        I could do business with a man of such honesty and sincerity. I \
        left feeling that we had become friends. I could speak Malay and \
        was to him more like an Indonesian peranakan (a local-born \
        Chinese), not a totok, a Chinese-speaking Chinese recent immigrant \
        who was less assimilated.";
    b.iter(|| feistel_encrypt(plaintext, 19652000, 158));
}

#[bench]
fn paragraph_decryption(b: &mut Bencher) {
    let ciphertext = vec![330914025, 318859736, 412327177, 85937750,
        361919662, 197263679, 341556854, 129742680, 49608046, 131431863,
         377645999, 514634703, 455375994, 389219293, 97654600, 30912823,
         289965619, 348169715, 514634703, 361244056, 377839881, 116251752,
         102661496, 254455057, 325750855, 443540390, 458679897, 434353452,
         298948700, 49715366, 131431863, 390359523, 282021303, 321943145,
         290037857, 220159197, 490640533, 56823382, 297287280, 365805751,
         281041611, 514634703, 451055360, 282426245, 399569183, 6714465,
         116251752, 469168721, 326568556, 297287280, 6247131, 328467303,
         308345456, 361919662, 341173642, 297032265, 60914938, 116251752,
         318371258, 530433696, 215699263, 326568556, 110953041, 275791260,
         19001912, 330789481, 490640533, 350259646, 390359523, 320273297,
         49715366, 399569183, 26111577, 341163414, 88884338, 331553931,
         47002870, 99054845, 10144064, 484987392, 237983559, 377645999,
         326971203, 490640533, 4036735, 290037857, 407367828, 99054845,
         9489425, 372793277, 115647571, 281041611, 397807620, 22151759,
         325104081, 320673346, 328084768, 330716423, 274271804, 220159197,
         297287280, 326971203, 100180692, 371158198, 16938891, 330582451,
         533206964, 192272880, 66690455, 309662246, 130591234, 434353452,
         12387937, 309662246, 130591234, 434353452, 298971690, 8332200,
         42313931, 272159424, 326971203, 322148104, 130591234, 115647571,
         330582451, 6714465, 325104081, 77423793, 336964353, 322806734,
         116251752, 91393782, 341173642, 470029505, 432980830, 332289651,
         99054845, 2960043, 303087019, 61093313, 322148104, 192272880,
         69158223, 192272880, 11564498, 16190001, 27896694, 357747396,
         88884338, 297032265, 19001912, 330601851, 130591234, 138078253,
         330582451, 488385983, 4036735, 126202297, 49715366, 361919662,
         117100194, 130591234, 66690455, 4036735, 53911493, 289427350,
         85937750, 97654600, 220159197, 285753993, 490640533, 56720929,
         192272880, 330601851, 23000043, 130591234, 91393782, 330582451,
         10333149, 440738352, 99054845, 238125335, 43105183, 52100186,
         303087019, 196493281, 401083533, 282687417, 399569183, 10144064,
         325384076, 482554350, 130591234, 11785978, 358256844, 11729210,
         327972824, 49715366, 227954397, 341977829, 282426245, 319813597,
         46759164, 313289604, 13121324, 57558329, 441487290, 77423793,
         107305229, 298948700, 192272880, 418824455, 275942764, 45818475,
         46766041, 26543742, 293863794, 322835079, 325096490, 298948700,
         325113476, 514634703, 15027768, 320673346, 130538782, 74076144,
         220159197, 49731554, 358827243, 25682762, 99054845, 54929716,
         396558, 500119412, 330601851, 11211864, 303087019, 371158198,
         396256549, 34263196, 394382455, 303087019, 80482495, 88884338,
         117100194, 40445119, 99054845, 311851980, 313289604, 98273486,
         330601851, 286327984, 530433696, 472302298, 444332987, 345072121,
         46075407, 275942764, 286375870, 115941106, 102661496, 107444566,
         8332200, 61093313, 10144064, 4140666, 99054845, 467227915,
         321943145, 337227774, 99054845, 344584319, 282021303, 307880714,
         399569183, 82250275, 305965227, 353712764, 58173397, 341556854,
         326089461, 192272880, 331082063, 13338785, 274271804, 338684040,
         99054845, 325750855, 60864858, 319139267, 419160192, 192272880,
         328602040, 325384076, 444332987, 169312202, 484987392, 88884338,
         13338785, 147981817, 275942764, 361919662, 391877736, 371967900,
         325384076, 371158198, 17920720, 37230147, 312440156, 10144064,
         321943145, 295232657, 26741201, 8332200, 325384076, 102661496,
         318376900, 490640533, 349672325, 46766041, 13338785, 10333149,
         8332200, 46766041, 497180185, 352831218, 215699263, 122979741,
         147981817, 399569183, 79627527, 59947939, 152419965, 365805751,
         131403329, 13730126, 350745242, 322910627, 100180692, 102661496,
         526527503, 306989162, 298948700, 85937750, 328602040, 192272880,
         298948700, 197263679, 36090316, 485845192, 325338623, 383359507,
         399569183, 34147019, 192272880, 359696040, 441951715, 22528899,
         293863794, 318631537, 13730126, 338684040, 275283939, 11785978,
         305423662, 303087019, 97654600, 24879021, 341556854, 430380115,
         298971690, 330789481, 79627527, 165915352, 4036735, 484987392,
         419160192, 326971203, 338684040, 99054845, 6714465, 309662246,
         10333149, 275791260, 325384076, 282932269, 399569183, 57854502,
         171666341, 41003298, 42313931, 358923734, 322035074, 341163414,
         325384076, 445478501, 130591234, 431458254, 134424934, 381210768,
         320673346, 337160881, 8332200, 91393782, 192272880, 357747396,
         116251752, 469168721, 345072121, 220159197, 467227915, 13730126,
         334499401, 42313931, 11211864, 334786611, 61093313, 272603824,
         282426245, 318631537, 281041611, 46, 321705776, 361168142,
         400306521, 27697993, 391751334, 294360632, 391825246, 866621,
         468299768, 220111639, 26932499, 92963609, 280335729, 375195708,
         260825036, 395554169, 536836718, 1838848, 92963609, 254138348,
         523227001, 129186667, 355621577, 145673166, 530233174, 50772916,
         76286662, 240248032, 208810582, 149434707, 220111639, 31090442,
         9028192, 391107170, 525347034, 271763241, 352209506, 496209462,
         174939521, 365316702, 460426326, 92963609, 523758046, 16924146,
         335109360, 33460616, 129186667, 12076303, 80329768, 174939521,
         209116646, 319684512, 499585521, 391751334, 430258821, 391876942,
         309129356, 129186667, 385051201, 168264357, 294332707, 80329768,
         14605507, 459275515, 99526385, 393602550, 352209506, 309169713,
         31090442, 288444504, 149434707, 335109360, 2361698, 221382741,
         364047315, 246523376, 186166420, 53087820, 103490700, 341259521,
         214712861, 26932499, 238716379, 352209506, 71357467, 525347034,
         524813498, 53087820, 75035528, 388269144, 129143147, 460426326,
         392425908, 322891922, 515459466, 347557762, 344457909, 380837331,
         427913718, 271763241, 174939521, 238716379, 15444985, 421066873,
         303540138, 383180816, 248737746, 286495271, 131775195, 510641819,
         91755482, 240248032, 183135353, 510641819, 91755482, 240248032,
         329997757, 389223751, 306932498, 270995013, 238716379, 297329809,
         91755482, 129143147, 383180816, 33460616, 515459466, 276492963,
         437707533, 327941902, 129186667, 64681413, 430258821, 239377139,
         29132775, 244944551, 53087820, 440979809, 4698962, 123218425,
         297329809, 286495271, 519636011, 286495271, 282860325, 266607736,
         168083813, 393128102, 364047315, 391876942, 99526385, 374865447,
         91755482, 488529200, 383180816, 385402865, 71357467, 12038143,
         149434707, 391751334, 296182078, 91755482, 131775195, 71357467,
         62930265, 368080566, 27697993, 260825036, 271763241, 132707654,
         352209506, 487380296, 286495271, 374865447, 88555900, 91755482,
         64681413, 383180816, 388796778, 316995211, 53087820, 7900878,
         32149732, 68988755, 4698962, 200125610, 465169909, 308758231,
         335109360, 103490700, 417834523, 515806306, 91755482, 82207532,
         20065878, 77839026, 122300814, 149434707, 489030577, 269247166,
         16924146, 363925500, 399843009, 92433792, 134169766, 501410354,
         9569280, 276492963, 130417071, 208810582, 286495271, 22478346,
         399993006, 17228683, 32636046, 289145903, 19124905, 295953723,
         516532933, 208810582, 516219782, 92963609, 7935096, 347557762,
         342461694, 129394535, 271763241, 20390801, 360259507, 311512467,
         53087820, 364480408, 504075288, 324391277, 374865447, 4226940,
         4698962, 421066873, 351864453, 335442314, 87997068, 4698962,
         261532531, 364047315, 296182078, 44564246, 53087820, 5674204,
         92433792, 47827038, 374865447, 376619607, 168264357, 74080930,
         407374021, 324706584, 305120247, 399993006, 374202276, 327282865,
         355621577, 476031411, 389223751, 123218425, 103490700, 312050818,
         53087820, 26224767, 391107170, 525623398, 53087820, 347910135,
         9028192, 113460501, 335109360, 39733224, 101527875, 112227604,
         95381853, 391825246, 503951255, 286495271, 332776403, 269135158,
         427913718, 475444108, 53087820, 530233174, 105327653, 351902106,
         258135099, 286495271, 325431796, 417834523, 407374021, 5510408,
         341259521, 364047315, 269135158, 357981025, 399993006, 391751334,
         185348001, 400656813, 417834523, 421066873, 367782595, 300139684,
         224722728, 103490700, 391107170, 75477912, 305261948, 389223751,
         417834523, 355621577, 386951961, 352209506, 441451197, 32636046,
         269135158, 388796778, 389223751, 32636046, 322754238, 325699901,
         294332707, 388800149, 357981025, 335109360, 53736132, 374430550,
         452782070, 365316702, 219966593, 388394927, 350500259, 298941396,
         15444985, 355621577, 8481483, 74990904, 208810582, 27697993,
         325431796, 286495271, 208810582, 294360632, 53126108, 495762520,
         506611812, 75218216, 335109360, 128105282, 286495271, 105704080,
         258896539, 400447998, 19124905, 358810810, 388394927, 475444108,
         434978309, 82207532, 93035258, 4698962, 260825036, 497013561,
         391825246, 66271620, 329997757, 393602550, 53736132, 48834938,
         71357467, 341259521, 258135099, 238716379, 475444108, 53087820,
         33460616, 510641819, 388796778, 459275515, 417834523, 261511065,
         335109360, 173402960, 196872525, 118460944, 306932498, 101705579,
         300108571, 221382741, 417834523, 18252849, 91755482, 76325797,
         44806872, 424943222, 347557762, 351231164, 389223751, 64681413,
         286495271, 393128102, 129186667, 12076303, 324706584, 271763241,
         26224767, 388394927, 42263811, 306932498, 4226940, 347985954,
         123218425, 429303437, 16924146, 358810810, 460426326];
    b.iter(|| {
        let cipher_clone = ciphertext.clone();
        feistel_decrypt(cipher_clone, 1965200, 158);
    });
}