This file is indexed.

/usr/share/perl5/Crypt/Util.pm is in libcrypt-util-perl 0.11-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
#!/usr/bin/perl

package Crypt::Util;
use Moose;

our $VERSION = "0.11";

use Digest;
use Digest::MoreFallbacks;

use Carp qw/croak/;

use Sub::Exporter;
use Data::OptList;

use namespace::clean -except => [qw(meta)];

our %DEFAULT_ACCESSORS = (
	mode                     => { isa => "Str" },
	authenticated_mode       => { isa => "Str" },
	encode                   => { isa => "Bool" },
	encoding                 => { isa => "Str" },
	printable_encoding       => { isa => "Str" },
	alphanumerical_encoding  => { isa => "Str" },
	uri_encoding             => { isa => "Str" },
	digest                   => { isa => "Str" },
	cipher                   => { isa => "Str" },
	mac                      => { isa => "Str" },
	key                      => { isa => "Str" },
	uri_encoding             => { isa => "Str" },
	printable_encoding       => { isa => "Str" },
	use_literal_key          => { isa => "Bool" },
	tamper_proof_unencrypted => { isa => "Bool" },
	nonce                    => { isa => "Str", default => "" },
);

our @DEFAULT_ACCESSORS = keys %DEFAULT_ACCESSORS;

my %export_groups = (
	'crypt' => [qw/
		encrypt_string decrypt_string
		authenticated_encrypt_string
		tamper_proof thaw_tamper_proof
		cipher_object
	/],
	digest => [qw/
		digest_string verify_hash verify_digest
		digest_object
		mac_digest_string
		verify_mac
	/],
	encoding => [qw/
		encode_string decode_string
		encode_string_hex decode_string_hex
		encode_string_base64 decode_string_base64 encode_string_base64_wrapped
		encode_string_base32 decode_string_base32
		encode_string_uri_base64 decode_string_uri_base64
		encode_string_uri decode_string_uri
		encode_string_alphanumerical decode_string_alphanumerical
		encode_string_printable decode_string_printable
		encode_string_uri_escape decode_string_uri_escape
	/],
	params => [ "exported_instance", "disable_fallback", map { "default_$_" } @DEFAULT_ACCESSORS ],
);

my %exports = map { $_ => \&__curry_instance } map { @$_ } values %export_groups;

Sub::Exporter->import( -setup => {
	exports    => \%exports,
	groups     => \%export_groups,
	collectors => {
		defaults => sub { 1 },
	},
});

our @KNOWN_AUTHENTICATING_MODES = qw(EAX OCB GCM CWC CCM); # IACBC & IAPM will probably never be implemented

our %KNOWN_AUTHENTICATING_MODES = map { $_ => 1 } @KNOWN_AUTHENTICATING_MODES;

our %FALLBACK_LISTS = (
	mode                    => [qw/CFB CBC Ctr OFB/],
	stream_mode             => [qw/CFB Ctr OFB/],
	block_mode              => [qw/CBC/],
	authenticated_mode      => [qw/EAX GCM CCM/], # OCB/], OCB is patented
	cipher                  => [qw/Rijndael Serpent Twofish RC6 Blowfish RC5/],
	#authenticated_cipher    => [qw/Phelix SOBER-128 Helix/], # not yet ready
	digest                  => [qw/SHA-1 SHA-256 RIPEMD160 Whirlpool MD5 Haval256/],
	mac                     => [qw/HMAC CMAC/],
	encoding                => [qw/hex/],
	printable_encoding      => [qw/base64 hex/],
	alphanumerical_encoding => [qw/base32 hex/],
	uri_encoding            => [qw/uri_base64 base32 hex/],
);

foreach my $fallback ( keys %FALLBACK_LISTS ) {
	my @list = @{ $FALLBACK_LISTS{$fallback} };

	my $list_method = "fallback_${fallback}_list";

	my $list_method_sub = sub { # derefed list accessors
		my ( $self, @args ) = @_;
		if ( @args ) {
			@args = @{ $args[0] } if @args == 1 and (ref($args[0])||'') eq "ARRAY";
			$self->{$list_method} = \@args;
		}
		@{ $self->{$list_method} || \@list };
	};

	my $type = ( $fallback =~ /(encoding|mode)/ )[0] || $fallback;
	my $try = "_try_${type}_fallback";

	my $fallback_sub = sub {
		my $self = shift;

		$self->_find_fallback(
			$fallback,
			$try,
			$self->$list_method,
		) || croak "Couldn't load any $fallback";
	};

	no strict 'refs';
	*{ "fallback_$fallback" } = $fallback_sub;
	*{ $list_method } = $list_method_sub;
}

foreach my $attr ( @DEFAULT_ACCESSORS ) {
	has "default_$attr" => (
		is => "rw",
		predicate => "has_default_$attr",
		clearer   => "clear_default_$attr",
		( __PACKAGE__->can("fallback_$attr") ? (
			lazy_build => 1,
			builder => "fallback_$attr",
		) : () ),
		%{ $DEFAULT_ACCESSORS{$attr} },
	);
}

has disable_fallback => (
	isa => "Bool",
	is  => "rw",
);

__PACKAGE__->meta->make_immutable if __PACKAGE__->meta->can("make_immutable");

{
	my %fallback_caches;

	sub _find_fallback {
		my ( $self, $key, $test, @list ) = @_;

		my $cache = $fallback_caches{$key} ||= {};

		@list = $list[0] if @list and $self->disable_fallback;

		foreach my $elem ( @list ) {
			$cache->{$elem} = $self->$test( $elem ) unless exists $cache->{$elem};
			return $elem if $cache->{$elem};
		}


		return;
	}
}

sub _try_cipher_fallback {
	my ( $self, $name ) = @_;
	$self->_try_loading_module("Crypt::$name");
}

sub _try_digest_fallback {
	my ( $self, $name ) = @_;

	my $e;

	{
		local $@;
		eval { $self->digest_object( digest => $name ) };
		$e = $@;
	};

	return 1 if !$e;
	( my $file = $name ) =~ s{::}{/}g;
	die $e if $e !~ m{^Can't locate Digest/\Q${file}.pm\E in \@INC};
	return;
}

sub _try_mode_fallback {
	my ( $self, $mode ) = @_;
	$self->_try_loading_module("Crypt::$mode");
}

sub _try_mac_fallback {
	my ( $self, $mac ) = @_;
	$self->_try_loading_module("Digest::$mac");
}

sub _try_loading_module {
	my ( $self, $name ) = @_;

	(my $file = "${name}.pm") =~ s{::}{/}g;

	my ( $r, $e );
	{
		local $@;
		$r = eval { require $file }; # yes it's portable
		$e = $@;
	};

	return $r if $r;
	die $e if $e !~ /^Can't locate \Q$file\E in \@INC/;
	return $r;
}

{
	my %encoding_module = (
		base64     => "MIME::Base64",
		uri_base64 => "MIME::Base64::URLSafe",
		base32     => "MIME::Base32",
		uri_escape => "URI::Escape",
	);

	sub _try_encoding_fallback {
		my ( $self, $encoding ) = @_;

		return 1 if $encoding eq "hex";

		my $module = $encoding_module{$encoding};
		$module =~ s{::}{/}g;
		$module .= ".pm";

		my $e = do {
			local $@;
			eval { require $module }; # yes it's portable
			$@;
		};

		return 1 if !$e;
		die $e if $e !~ /^Can't locate \Q$module\E in \@INC/;
		return;
	}
}

sub _args (\@;$) {
	my ( $args, $odd ) = @_;

	my ( $self, @args ) = @$args;

	my %params;
	if ( @args % 2 == 1 ) {
		croak "The parameters must be an even sized list of key value pairs" unless defined $odd;
		( my $odd_value, %params ) = @args;
		croak "Can't provide the positional param in the named list as well" if exists $params{$odd};
		$params{$odd} = $odd_value;
	} else {
		%params = @args;
	}

	return ( $self, %params );
}

sub _process_params {
	my ( $self, $params, @required ) = @_;

	foreach my $param ( @required ) {
		next if exists $params->{$param};

		$params->{$param} = $self->_process_param( $param );
	}
}

sub _process_param {
	my ( $self, $param ) = @_;

	my $default = "default_$param";

	if ( $self->can($default) ) {
		return $self->$default;
	}

	croak "No default value for required parameter '$param'";
}

sub cipher_object {
	my ( $self, %params ) = _args @_;

	$self->_process_params( \%params, qw/mode/);

	my $method = "cipher_object_" . lc(my $mode = delete $params{mode});

	croak "mode $mode is unsupported" unless $self->can($method);

	$self->$method( %params );
}

sub cipher_object_eax {
	my ( $self, %params ) = _args @_;

	$self->_process_params( \%params, qw/cipher nonce/ );

	require Crypt::EAX;

	Crypt::EAX->new(
		%params,
		cipher => "Crypt::$params{cipher}", # FIXME take a ref, but Crypt::CFB will barf
		key    => $self->process_key(%params),
		nonce  => $params{nonce},
	);
}

sub cipher_object_cbc {
	my ( $self, %params ) = _args @_;

	$self->_process_params( \%params, qw/cipher/ );

	require Crypt::CBC;

	Crypt::CBC->new(
		-cipher      => $params{cipher},
		-key         => $self->process_key(%params),
	);
}

sub cipher_object_ofb {
	my ( $self, %params ) = _args @_;

	$self->_process_params( \%params, qw/cipher/ );

	require Crypt::OFB;
	my $c = Crypt::OFB->new;

	$c->padding( Crypt::ECB::PADDING_AUTO() );

	$c->key( $self->process_key(%params) );

	$c->cipher( $params{cipher} );

	return $c;
}

sub cipher_object_cfb {
	my ( $self, @args ) = _args @_;
	require Crypt::CFB;
	$self->_cipher_object_baurem( "Crypt::CFB", @args );
}

sub cipher_object_ctr {
	my ( $self, @args ) = _args @_;
	require Crypt::Ctr;
	$self->_cipher_object_baurem( "Crypt::Ctr", @args );
}

sub _cipher_object_baurem {
	my ( $self, $class, %params ) = @_;

	my $prefix = "Crypt";
	( $prefix, $params{cipher} ) = ( Digest => delete $params{digest} ) if exists $params{encryption_digest};

	$self->_process_params( \%params, qw/cipher/ );

	$class->new( $self->process_key(%params), join("::", $prefix, $params{cipher}) );
}

use tt;
[% FOR mode IN ["stream", "block", "authenticated"] %]
sub cipher_object_[% mode %] {
	my ( $self, @args ) = _args @_;
	my $mode = $self->_process_param("[% mode %]_mode");
	$self->cipher_object( @args, mode => $mode );
}
[% END %]
no tt;

sub process_nonce {
	my ( $self, %params ) = _args @_, 'nonce';

	my $nonce = $self->_process_params( \%params, 'nonce' );

	if ( length($nonce) ) {
		return $nonce;
	} else {
		require Data::GUID;
		Data::GUID->new->as_binary;
	}
}

sub process_key {
	my ( $self, %params ) = _args @_, "key";

	if ( $params{literal_key} || $self->default_use_literal_key ) {
		$self->_process_params( \%params, qw/key/ );
		return $params{key};
	} else {
		my $size = $params{key_size};

		unless ( $size ) {
			$self->_process_params( \%params, qw/key cipher/ );
			my $cipher = $params{cipher};

			my $class = "Crypt::$cipher";

			$self->_try_loading_module($class);

			if ( my $size_method = $class->can("keysize") || $class->can("blocksize") ) {
				$size = $class->$size_method;
			}

			$size ||= $cipher eq "Blowfish" ? 56 : 32;
		}

		return $self->digest_string(
			string => $params{key},
			digest => "MultiHash",
			encode => 0,
			digest_args => [{
				width  => $size,
				hashes => ["SHA-512"], # no need to be overkill, we just need the variable width
			}],
		);
	}
}

sub digest_object {
	my ( $self, %params ) = _args @_;

	$self->_process_params( \%params, qw/
		digest
	/);

	Digest->new( $params{digest}, @{ $params{digest_args} || [] } );
}

{
	# this is a hack that gives to Digest::HMAC something that responds to ->new

	package
	Crypt::Util::HMACDigestFactory;

	sub new {
		my $self = shift;
		$$self->clone;
	}

	sub new_factory {
		my ( $self, $thing ) = @_;
		return bless \$thing, $self;
	}
}

sub mac_object {
	my ( $self, %params ) = _args @_;

	$self->_process_params( \%params, qw/
		mac
	/);

	my $mac_type = delete $params{mac};

	my $method = lc( "mac_object_$mac_type" );

	$self->$method( %params );
}

sub mac_object_hmac {
	my ( $self, @args ) = _args @_;

	my $digest = $self->digest_object(@args);

	my $digest_factory = Crypt::Util::HMACDigestFactory->new_factory( $digest );

	my $key = $self->process_key(
		literal_key => 1, # Digest::HMAC does it's own key processing, but we let the user force our own
		key_size => 64,   # if the user did force our own, the default key_size is Digest::HMAC's default block size
		@args,
	);

	require Digest::HMAC;
	Digest::HMAC->new(
		$key,
		$digest_factory,
		# FIXME hmac_block_size param?
	);
}

sub mac_object_cmac {
	my ( $self, %params ) = _args @_;

	my ( $key, $cipher );

	if ( ref $params{cipher} ) {
		$cipher = $params{cipher};
	} else {
		$self->_process_params( \%params, qw(cipher) );
		$cipher = "Crypt::" . $params{cipher};
		$key    = $self->process_key(%params);
	}

	require Digest::CMAC;
	Digest::CMAC->new( $key, $cipher );
}

use tt;
[% FOR f IN ["en", "de"] %]
sub [% f %]crypt_string {
	my ( $self, %params ) = _args @_, "string";

	my $string = delete $params{string};
	croak "You must provide the 'string' parameter" unless defined $string;

	my $c = $self->cipher_object( %params );

	[% IF f == "en" %]
	$self->maybe_encode( $c->encrypt($string), \%params );
	[% ELSE %]
	$c->decrypt( $self->maybe_decode($string, \%params ) );
	[% END %]
}

sub maybe_[% f %]code {
	my ( $self, $string, $params ) = @_;

	my $should_encode = exists $params->{[% f %]code}
		? $params->{[% f %]code}
		: exists $params->{encoding} || $self->default_encode;

	if ( $should_encode ) {
		return $self->[% f %]code_string(
			%$params,
			string   => $string,
		);
	} else {
		return $string;
	}
}
[% END %]
no tt;

sub _digest_string_with_object {
	my ( $self, $object, %params ) = @_;

	my $string = delete $params{string};
	croak "You must provide the 'string' parameter" unless defined $string;

	$object->add($string);

	$self->maybe_encode( $object->digest, \%params );
}

sub digest_string {
	my ( $self, %params ) = _args @_, "string";

	my $d = $self->digest_object( %params );

	$self->_digest_string_with_object( $d, %params );
}

sub mac_digest_string {
	my ( $self, %params ) = _args @_, "string";

	my $d = $self->mac_object( %params );

	$self->_digest_string_with_object( $d, %params );
}

sub _do_verify_hash {
	my ( $self, %params ) = _args @_;

	my $hash = delete $params{hash};
	my $fatal = delete $params{fatal};
	croak "You must provide the 'string' and 'hash' parameters" unless defined $params{string} and defined $hash;

	my $meth = $params{digest_method};

	return 1 if $hash eq $self->$meth(%params);

	if ( $fatal ) {
		croak "Digest verification failed";
	} else {
		return;
	}
}

sub verify_hash {
	my ( $self, @args ) = @_;
	$self->_do_verify_hash(@args, digest_method => "digest_string");
}

sub verify_digest {
	my ( $self, @args ) = @_;
	$self->verify_hash(@args);
}

sub verify_mac {
	my ( $self, @args ) = @_;
	$self->_do_verify_hash(@args, digest_method => "mac_digest_string");
}

{
	my @flags = qw/serialized/;

	sub _flag_hash_to_int {
		my ( $self, $flags ) = @_;

		my $bit = 1;
		my $flags_int = 0;

		foreach my $flag (@flags) {
			$flags_int |= $bit if $flags->{$flag};
		} continue {
			$bit *= 2;
		}

		return $flags_int;
	}

	sub _flag_int_to_hash {
		my ( $self, $flags ) = @_;

		my $bit =1;
		my %flags;

		foreach my $flag (@flags ) {
			$flags{$flag} = $flags & $bit;
		} continue {
			$bit *= 2;
		}

		return wantarray ? %flags : \%flags;
	}
}

sub tamper_proof {
	my ( $self, %params ) = _args @_, "data";

	$params{string} = $self->pack_data( %params );
	#$params{header} = $self->pack_data( %params, data => $params{header} ) if exists $params{header}; # FIXME this is not yet finished

	$self->tamper_proof_string( %params );
}

sub freeze_data {
	my ( $self, %params ) = @_;
	require Storable;
	Storable::nfreeze($params{data});
}

sub thaw_data {
	my ( $self, %params ) = @_;
	require Storable;
	Storable::thaw($params{data});
}

sub tamper_proof_string {
	my ( $self, %params ) = _args @_, "string";

	my $encrypted = exists $params{encrypt}
		? $params{encrypt}
		: !$self->default_tamper_proof_unencrypted;


	my $type = ( $encrypted ? "aead" : "mac" );

	my $method = "${type}_tamper_proof_string";

	my $string = $self->$method( %params );

	return $self->_pack_tamper_proof( $type => $string );
}

{
	my @tamper_proof_types = qw/mac aead/;
	my %tamper_proof_type; @tamper_proof_type{@tamper_proof_types} = 1 .. @tamper_proof_types;

	sub _pack_tamper_proof {
		my ( $self, $type, $proof ) = @_;
		pack("C a*", $tamper_proof_type{$type}, $proof);
	}

	sub _unpack_tamper_proof {
		my ( $self, $packed ) = @_;
		my ( $type, $string ) = unpack("C a*", $packed);

		return (
			($tamper_proof_types[ $type-1 ] || croak "Unknown tamper proofing method"),
			$string,
		);
	}
}

sub _authenticated_mode {
	my ( $self, $params ) = @_;

	# trust explicit param
	if ( exists $params->{authenticated_mode} ) {
		$params->{mode} = delete $params->{authenticated_mode};
		return 1;
	}

	# check if the explicit param is authenticated
	if ( exists $params->{mode} ) {
		# allow overriding
		if ( exists $params->{mode_is_authenticated} ) {
			return $params->{mode_is_authenticated};
		}

		if ( $KNOWN_AUTHENTICATING_MODES{uc($params->{mode})} ) {
			return 1;
		} else {
			return;
		}
	}

	$params->{mode} = $self->_process_param('authenticated_mode');

	return 1;
}

sub _pack_hash_and_message {
	my ( $self, $hash, $message ) = @_;
	pack("n/a* a*", $hash, $message);
}

sub _unpack_hash_and_message {
	my ( $self, $packed ) = @_;
	unpack("n/a* a*", $packed);
}

our $PACK_FORMAT_VERSION = 1;

sub pack_data {
	my ( $self, %params ) = _args @_, "data";

	$self->_process_params( \%params, qw/
		data
	/);

	my $data = delete $params{data};

	my %flags;

	if ( ref $data ) {
		$flags{serialized} = 1;
		$data = $self->freeze_data( %params, data => $data );
	}

	$self->_pack_version_flags_and_string( $PACK_FORMAT_VERSION, \%flags, $data );
}

sub unpack_data {
	my ( $self, %params ) = _args @_, "data";

	$self->_process_params( \%params, qw/
		data
	/);

	my ( $version, $flags, $data ) = $self->_unpack_version_flags_and_string($params{data});

	$self->_packed_string_version_check( $version );

	if ( $flags->{serialized} ) {
		return $self->thaw_data( %params, data => $data );
	} else {
		return $data;
	}
}

sub _pack_version_flags_and_string {
	my ( $self, $version, $flags, $string ) = @_;
	pack("n n N/a*", $version, $self->_flag_hash_to_int($flags), $string);
}

sub _unpack_version_flags_and_string {
	my ( $self, $packed ) = @_;

	my ( $version, $flags, $string ) = unpack("n n N/a*", $packed);

	$flags = $self->_flag_int_to_hash($flags);

	return ( $version, $flags, $string );
}

sub authenticated_encrypt_string {
	my ( $self, %params ) = _args @_, "string";

	# FIXME some ciphers are authenticated, but none are implemented in perl yet
	if ( $self->_authenticated_mode(\%params) ) {
		$self->_process_params( \%params, qw(nonce) );

		# generate a nonce unless one is explicitly provided
		my $nonce = $self->process_nonce(%params); # FIMXE limit to 64k?

		# FIXME safely encode an arbitrary header as well
		#my $header = $params{header};
		#$header = '' unless defined $header;

		return pack("n/a* a*", $nonce, $self->encrypt_string( %params, nonce => $nonce ) );
	} else {
		croak "To use encrypted tamper resistent strings an authenticated encryption mode such as EAX must be selected";
	}
}

sub authenticated_decrypt_string {
	my ( $self, %params ) = _args @_, "string";

	if ( $self->_authenticated_mode(\%params) ) {
		$self->_process_params( \%params, qw(string) );

		my ( $nonce, $string ) = unpack("n/a* a*", $params{string});

		return $self->decrypt_string(
			fatal => 1,
			%params,
			nonce  => $nonce,
			string => $string,
		);
	} else {
		croak "To use encrypted tamper resistent strings an authenticated encryption mode such as EAX must be selected";
	}
}

sub aead_tamper_proof_string {
	my ( $self, %params ) = _args @_, "string";

	$self->authenticated_encrypt_string( %params );
}

sub mac_tamper_proof_string {
	my ( $self, %params ) = _args @_, "string";

	my $string = delete $params{string};
	croak "You must provide the 'string' parameter" unless defined $string;

	my $hash = $self->mac_digest_string(
		%params,
		encode => 0,
		string => $string,
	);

	return $self->_pack_hash_and_message( $hash, $string );
}

sub thaw_tamper_proof_string {
	my ( $self, %params ) = _args @_, "string";

	my $string = delete $params{string};
	croak "You must provide the 'string' parameter" unless defined $string;

	my ( $type, $message ) = $self->_unpack_tamper_proof($string);

	my $method = "thaw_tamper_proof_string_$type";

	my $packed = $self->$method( %params, string => $message );
}

sub thaw_tamper_proof {
	my ( $self, %params ) = _args @_, "string";
	my $packed = $self->thaw_tamper_proof_string(%params);
	$self->unpack_data(%params, data => $packed);
}

sub thaw_tamper_proof_string_aead {
	my ( $self, %params ) = _args @_, "string";

	$self->authenticated_decrypt_string( %params );
}

sub thaw_tamper_proof_string_mac {
	my ( $self, %params ) = _args @_, "string";

	my $hashed_packed = delete $params{string};
	croak "You must provide the 'string' parameter" unless defined $hashed_packed;

	my ( $hash, $packed ) = $self->_unpack_hash_and_message( $hashed_packed );

	return unless $self->verify_mac(
		fatal  => 1,
		%params,
		hash   => $hash,
		decode => 0,
		string => $packed,
	);

	return $packed;
}

sub _packed_string_version_check {
	my ( $self, $version ) = @_;

	croak "Incompatible packed string (I'm version $PACK_FORMAT_VERSION, thawing version $version)"
		unless $version == $PACK_FORMAT_VERSION;
}

use tt;
[% FOR f IN ["en","de"] %]
sub [% f %]code_string {
	my ( $self, %params ) = _args @_, "string";

	my $string = delete $params{string};
	croak "You must provide the 'string' parameter" unless defined $string;

	$self->_process_params( \%params, qw/
		encoding
	/);

	my $encoding = delete $params{encoding};
	croak "Encoding method must be an encoding name" unless $encoding;
	my $method = "[% f %]code_string_$encoding";
	croak "Encoding method $encoding is not supported" unless $self->can($method);

	$self->$method($string);
}
[% END %]
no tt;

sub encode_string_hex {
	my ( $self, $string ) = @_;
	unpack("H*", $string);
}

sub decode_string_hex {
	my ( $self, $hex ) = @_;
	pack("H*", $hex );
}

sub encode_string_base64 {
	my ( $self, $string ) = @_;
	require MIME::Base64;
	MIME::Base64::encode_base64($string, "");
}

sub encode_string_base64_wrapped {
	my ( $self, $string ) = @_;
	require MIME::Base64;
	MIME::Base64::encode_base64($string);
}

sub decode_string_base64 {
	my ( $self, $base64 ) = @_;
	require MIME::Base64;
	MIME::Base64::decode_base64($base64);
}

# http://www.dev411.com/blog/2006/10/02/encoding-hashed-uids-base64-vs-hex-vs-base32
sub encode_string_uri_base64 {
	my ( $self, $string ) = @_;
	require MIME::Base64::URLSafe;
	MIME::Base64::URLSafe::encode($string);
}

sub decode_string_uri_base64 {
	my ( $self, $base64 ) = @_;
	require MIME::Base64::URLSafe;
	MIME::Base64::URLSafe::decode($base64);
}

sub encode_string_base32 {
	my ( $self, $string ) = @_;
	require MIME::Base32;
	MIME::Base32::encode_rfc3548($string);
}

sub decode_string_base32 {
	my ( $self, $base32 ) = @_;
	require MIME::Base32;
	MIME::Base32::decode_rfc3548(uc($base32));
}

sub encode_string_uri_escape {
	my ( $self, $string ) = @_;
	require URI::Escape;
	URI::Escape::uri_escape($string);
}

sub decode_string_uri_escape {
	my ( $self, $uri_escaped ) = @_;
	require URI::Escape;
	URI::Escape::uri_unescape($uri_escaped);
}

use tt;
[% FOR symbolic_encoding IN ["uri", "alphanumerical", "printable"] %]
[% FOR f IN ["en", "de"] %]
sub [% f %]code_string_[% symbolic_encoding %] {
	my ( $self, $string ) = @_;
	my $encoding = $self->_process_param("[% symbolic_encoding %]_encoding");
	$self->[% f %]code_string( string => $string, encoding => $encoding );
}
[% END %]
[% END %]
no tt;

sub exported_instance {
	my $self = shift;
	return $self;
}

sub __curry_instance {
	my ($class, $method_name, undef, $col) = @_;

	my $self = $col->{instance} ||= $class->__curry_flavoured_instance($col);

	sub { $self->$method_name(@_) };
}

sub __curry_flavoured_instance {
	my ( $class, $col ) = @_;

	my %params; @params{ map { "default_$_" } keys %{ $col->{defaults} } } = values %{ $col->{defaults} };

	$class->new( \%params );
}

__PACKAGE__;

__END__

=pod

=head1 NAME

Crypt::Util - A lightweight Crypt/Digest convenience API

=head1 SYNOPSIS

	use Crypt::Util; # also has a Sub::Exporter to return functions wrapping a default instance

	my $util = Crypt::Util->new;

	$util->default_key("my secret");

	# MAC or cipher+digest based tamper resistent encapsulation
	# (uses Storable on $data if necessary)
	my $tamper_resistent_string = $util->tamper_proof( $data );

	my $verified = $util->thaw_tamper_proof( $untrusted_string, key => "another secret" );

	# If the encoding is unspecified, base32 is used
	# (hex if base32 is unavailable)
	my $encoded = $util->encode_string( $bytes );

	my $hash = $util->digest( $bytes, digest => "md5" );

	die "baaaad" unless $util->verify_hash(
		hash   => $hash,
		data   => $bytes,
		digest => "md5",
	);


=head1 DESCRIPTION

This module provides an easy, intuitive and forgiving API for wielding
crypto-fu.

The API is designed as a cascade, with rich features built using simpler ones.
this means that the option processing is uniform throughout, and the behaviors
are generally predictable.

Note that L<Crypt::Util> doesn't do any crypto on its own, but delegates the
actual work to the various other crypto modules on the CPAN. L<Crypt::Util>
merely wraps these modules, providing uniform parameters, and building on top
of their polymorphism with higher level features.

=head2 Priorities

=over 4

=item Ease of use

This module is designed to have an easy API to allow easy but responsible
use of the more low level Crypt:: and Digest:: modules on CPAN.  Therefore,
patches to improve ease-of-use are very welcome.

=item Pluggability

Dependency hell is avoided using a fallback mechanism that tries to choose an
algorithm based on an overridable list.

For "simple" use install Crypt::Util and your favourite digest, cipher and
cipher mode (CBC, CFB, etc).

To ensure predictable behavior the fallback behavior can be disabled as necessary.

=back

=head2 Interoperability

To ensure that your hashes and strings are compatible with L<Crypt::Util>
deployments on other machines (where different Crypt/Digest modules are
available, etc) you should use C<disable_fallback>.

Then either set the default ciphers, or always explicitly state the cipher.

If you are only encrypting and decrypting with the same installation, and new
cryptographic modules are not being installed, the hashes/ciphertexts should be
compatible without disabling fallback.

=head1 EXPORTED API

B<NOTE>: nothing is exported by default.

L<Crypt::Util> also presents an optional exported api using L<Sub::Exporter>.

Unlike typical exported APIs, there is no class level default instance shared
by all the importers, but instead every importer gets its own instance.

For example:

    package A;
    use Crypt::Util qw/:all/;

    default_key("moose");
    my $ciphertext = encrypt_string($plain);


    package B;
    use Crypt::Util qw/:all/;

    default_key("elk");
    my $ciphertext = encrypt_string($plain);

In this example every importing package has its own implicit instance, and the
C<default_key> function will in fact not share the value.

You can get the instance using the C<exported_instance> function, which is just
the identity method.

The export tags supported are: C<crypt> (encryption and tamper proofing related
functions), C<digest> (digest and MAC related functions), C<encoding> (various
encoding and decoding functions), and C<params> which give you functions for
handling default values.

=head1 METHODS

=over 4

=item tamper_proof( [ $data ], %params )

=item thaw_tamper_proof( [ $string ], %params )

=item tamper_proof_string( $string, %params )

=item thaw_tamper_proof_string( $string, %params )

=item aead_tamper_proof_string( [ $string ], %params )

=item mac_tamper_proof_string( [ $string ], %params )

The C<tamper_proof> method is in an intermittent state, in that the C<data>
parameter's API is not completely finalized.

It is safer to use C<tamper_proof_string>; its API is expected to remain the
same in future versions as well.

See L</TODO> for more information about the data types that will be supported
in the future.

When thawing, the C<authenticated_decrypt_string> or C<verify_mac> methods will
be used, with C<fatal> defaulting to on unless explicitly disabled in the
parameters.

=over 4

This method accepts the following parameters:

=item * encrypt

By default this parameter is true, unless C<default_tamper_proof_unencrypted()>,
has been enabled.

A true value implies that all the parameters
which are available to C<authenticated_encrypt_string()> are also available.
If a negative value is specified, MAC mode is used, and the additional
parameters of C<mac_digest_string()> may also be specified to this method.

=item * data

The data to encrypt. If this is a reference L<Storable> will be used to
serialize the data.

See C<pack_data> for details.

=back

If the string is encrypted then all the parameters of C<encrypt_string> and
C<digest_string> are also available.

If the string is not encrypted, then all the parameters of C<mac_digest_string>
are also available.

=item encrypt_string( [ $string ], %params )

=item decrypt_string( [ $string ], %params )

=item authenticated_encrypt_string( [ $string ], %params )

=item authenticated_decrypt_string( [ $string ], %params )

All of the parameters which may be supplied to C<process_key()>,
C<cipher_object> and C<maybe_encode> are also available to these methods.

The C<authenticated> variants ensure that an authenticated encryption mode
(such as EAX) is used.

The following parameters may be used:

=over 4

=item * string

The string to be en/decrypted can either be supplied first, creating an odd
number of arguments, or as a named parameter.

=item * nonce

The cryptographic nonce to use. Only necessary for encryption, will be packed
in the string as part of the message if applicable.

=item * header

Not yet supported.

In the future this will include a header for AEAD (the "associated data" bit of
AEAD).

=back

=item process_key( [ $key ], %params )

The following arguments may be specified:

=over 4

=item * literal_key

This disables mungung. See also C<default_use_literal_key>.

=item * key_size

Can be used to force a key size, even if the cipher specifies another size.

If not specified, the key size chosen will depend 

=item * cipher

Used to determine the key size.

=back

=item process_nonce( [ $nonce ], %params )

If a nonce is explicitly specified this method returns that, and otherwise uses
L<Data::GUID> to generate a unique binary string for use as a nonce/IV.

=item pack_data( [ $data ], %params )

=item unpack_data( [ $data ], %params )

Uses L<Storable> and C<pack> to create a string out of data.

L<MooseX::Storage> support will be added in the future.

The format itself is versioned in order to facilitate future proofing and
backwards compatibility.

Note that it is not safe to call C<unpack_data> on an untrusted string, use
C<thaw_tamper_proof> instead (it will authenticate the data and only then
perform the potentially unsafe routines).

=item cipher_object( %params )

Available parameters are:

=over 4

=item * cipher

The cipher algorithm to use, e.g. C<Rijndael>, C<Twofish> etc.

=item * mode

The mode of operation. This can be real (C<cbc>, C<cfb>, C<ctr>, C<ofb>,
C<eax>) or symbolic (C<authenticated>, C<block>, C<stream>).

See L<http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation> for an
explanation of this.

=back

=item cipher_object_eax( %params )

Used by C<cipher_object> but accepts additional parameters:

=over 4

=item * nonce

The nonce is a value that should be unique in order to protect against replay
attacks. It also ensures that the same plain text with the same key will
produce different ciphertexts.

The nonce is not included in the output ciphertext. See
C<authenticated_encrypt_string> for a convenience method that does include the
nonce.

=item * header

This is additional data to authenticate but not encrypt.

See L<Crypt::EAX> for more details.

The header will not be included in the output ciphertext.

=back

=item digest_string( [ $string ], %params )

Delegates to C<digest_object>. All parameters which can be used by
C<digest_object> may also be used here.

The following arguments are available:

=over 4

=item * string

The string to be digested can either be supplied first, creating an odd
number of arguments, or as a named parameter.

=back

=item verify_digest( %params )

Delegates to C<digest_object>. All parameters which can be used by
C<digest_object> may also be used here.

The following parameters are accepted:

=over 4

=item * hash

A string containing the hash to verify.

=item * string

The digested string.

=item * fatal

If true, errors will be fatal.  The default is false, which means that
failures will return undef.

=back

In addition, the parameters which can be supplied to C<digest_string()>
may also be supplied to this method.

=item digest_object( %params )

=over 4

=item * digest

The digest algorithm to use.

=back

Returns an object using L<Digest>.

=item encode_string( [ $string ], %params )

=item decode_string( [ $string ], %params )

The following parameters are accepted:

=over 4

=item * encoding

The encoding may be a symbolic type (uri, printable) or a concrete type
(none, hex, base64, base32).

=back

=item mac_digest_string( [ $string ], %param )

Delegates to C<mac_object>. All parameters which can be used by C<mac_object>
may also be used here.

=over 4

=item * string

=back

=item verify_mac( %params )

Delegates to C<mac_object>. All parameters which can be used by C<mac_object>
may also be used here.

The following additional arguments are allowed:

=over 4

=item * hash

The MAC string to verify.

=item * string

The digested string.

=item * fatal

If true, errors will be fatal.  The default is false, which means that
failures will return undef.

=back

=item mac_object( %params )

=over 4

=item * mac

The MAC algorithm to use. Currently C<hmac> and C<cmac> are supported.

=back

=item maybe_encode

=item maybe_decode

This method has no external API but is documented for the sake of its shared
options.

It is delegated to by the various encryption and digest method.

=over 4

=item * encode

Expects a bool.

=item * encoding

Expects an algorithm name (symbolic (e.g. C<uri>, C<alphanumeric>), or concrete
(e.g. C<base64>, C<hex>)).

=back

If C<encode> is explicitly supplied it will always determine whether or not the
string will be encoded. Otherwise, if C<encoding> is explicitly supplied then
the string will always be encoded using the specified algorithm. If neither is
supplied C<default_encode> will be checked to determine whether or not to
encode, and C<default_encoding> or C<fallback_encoding> will be used to
determine the algorithm to use (see L</HANDLING OF DEFAULT VALUES>).

=item encode_string_alphanumerical( $string )

=item decode_string_alphanumerical( $string )

=item encode_string_uri( $string )

=item decode_string_uri( $string )

=item encode_string_printable( $string )

=item decode_string_printable( $string )

The above methods encode based on a fallback list (see L</HANDLING OF DEFAULT VALUES>).

The variations denote types of formats: C<alphanumerical> is letters and
numbers only (case insensitive), C<uri> is safe for inclusions in URIs (without
further escaping), and C<printable> contains no control characters or
whitespace.

=item encode_string_hex( $string )

=item decode_string_hex( $string )

Big endian hexadecimal (C<H*> pack format).

=item encode_string_uri_escape( $string )

=item decode_string_uri_escape( $string )

L<URI::Escape> based encoding.

=item encode_string_base64( $string )

=item decode_string_base64( $string )

=item encode_string_base64_wrapped( $string )

Requires L<MIME::Base64>.

The C<wrapped> variant will introduce line breaks as per the L<MIME::Base64>
default>.

=item encode_string_uri_base64

=item decode_string_uri_base64

Requires L<MIME::Base64>.

Implements the Base64 for URIs. See
L<http://en.wikipedia.org/wiki/Base64#URL_Applications>.

=item encode_string_base32( $string )

=item decode_string_base32( $string )

Requires L<MIME::Base32>.

(note- unlike L<MIME::Base32> this is case insensitive).

=head1 HANDLING OF DEFAULT VALUES

=over 4

=item disable_fallback()

When true only the first item from the fallback list will be tried, and if it
can't be loaded there will be a fatal error.

Enable this to ensure portability.

=back

For every parameter, there are several methods, where PARAMETER is replaced
with the parameter name:

=over 4

=item * default_PARAMETER()

This accessor is available for the user to override the default value.

If set to undef, then C<fallback_PARAMETER> will be consulted instead.

B<ALL> the default values are set to undef unless changed by the user.

=item * fallback_PARAMETER()

Iterates the C<fallback_PARAMETER_list>, choosing the first value that is
usable (it's provider is available).

If C<disable_fallback> is set to a true value, then only the first value in the
fallback list will be tried.

=item * fallback_PARAMETER_list()

An ordered list of values to try and use as fallbacks.

C<fallback_PARAMETER> iterates this list and chooses the first one that works.

=back

Available parameters are as follows:

=over 4

=item * cipher

The fallback list is
C<Rijndael>, C<Serpent>, C<Twofish>, C<RC6>, C<Blowfish> and C<RC5>.

L<Crypt::Rijndael> is the AES winner, the next three are AES finalists, and the
last two are well known and widely used.

=item * mode

The mode in which to use the cipher.

The fallback list is C<CFB>, C<CBC>, C<Ctr>, and C<OFB>.

=item digest

The fallback list is C<SHA-1>, C<SHA-256>, C<RIPEMD160>,
C<Whirlpool>, C<MD5>, and C<Haval256>.

=item * encoding

The fallback list is C<hex> (effectively no fallback).

=item alphanumerical_encoding

The fallback list is C<base32> and C<hex>.

L<MIME::Base32> is required for C<base32> encoding.

=item * uri_encoding

The fallback list is C<uri_base64>.

=item * printable_encoding

The fallback list is C<base64>

=back

=head2 Defaults with no fallbacks

The following parameters have a C<default_> method, as described in the
previous section, but the C<fallback_> methods are not applicable.

=over 4

=item * encode

Whether or not to encode by default (applies to digests and encryptions).

=item * key

The key to use. Useful for when you are repeatedly encrypting.

=item * nonce

The nonce/IV to use for cipher modes that require it.

Defaults to the empty string, but note that some methods will generate a nonce
for you (e.g. C<authenticated_encrypt_string>) if none was provided.

=item * use_literal_key

Whether or not to not hash the key by default. See C<process_key>.

=item * tamper_proof_unencrypted

Whether or not tamper resistent strings are by default unencrypted (just MAC).

=back

=head2 Subclassing

You may safely subclass and override C<default_PARAMETER> and
C<fallback_PARAMETER_list> to provide values from configurations.

=back

=head1 TODO

=over 4

=item *

Crypt::SaltedHash support

=item *

EMAC (maybe, the modules are not OO and require refactoring) message
authentication mode

=item *

Bruce Schneier Fact Database
L<http://geekz.co.uk/lovesraymond/archive/bruce-schneier-facts>.

L<WWW::SchneierFacts>

=item *

Entropy fetching (get N weak/strong bytes, etc) from e.g. OpenSSL bindings,
/dev/*random, and EGD.

=item *

Additional data formats (streams/iterators, filehandles, generalized storable
data/string handling for all methods, not just tamper_proof).

Streams should also be able to used via a simple push api.

=item *

IV/nonce/salt support for the various cipher modes, not just EAX (CBC, CCM, GCM, etc)

=item *

L<Crypt::Rijndael> can do its own cipher modes

=head1 SEE ALSO

L<Digest>, L<Crypt::CBC>, L<Crypt::CFB>,
L<http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation>.

=head1 VERSION CONTROL

This module is maintained using Darcs. You can get the latest version from
L<http://nothingmuch.woobling.org/Crypt-Util/>, and use C<darcs send> to commit
changes.

=head1 AUTHORS

Yuval Kogman, E<lt>nothingmuch@woobling.orgE<gt>
Ann Barcomb

=head1 COPYRIGHT & LICENSE

Copyright 2006-2008 by Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>, Ann Barcomb

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

=cut