|
1 # |
|
2 # Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 # All rights reserved. |
|
4 # This component and the accompanying materials are made available |
|
5 # under the terms of the License "Eclipse Public License v1.0" |
|
6 # which accompanies this distribution, and is available |
|
7 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 # |
|
9 # Initial Contributors: |
|
10 # Nokia Corporation - initial contribution. |
|
11 # |
|
12 # Contributors: |
|
13 # |
|
14 # Description: |
|
15 # |
|
16 |
|
17 use strict; |
|
18 use Getopt::Long; |
|
19 my %opt; |
|
20 |
|
21 |
|
22 # HASH MD2/MD4/MD5/SHA1 Digest Generator |
|
23 |
|
24 #----------Define Function Headers---------- |
|
25 |
|
26 #Hash Functions |
|
27 use Digest::MD2 qw(md2 md2_hex md2_base64); # Copy-right 1998-2001 Gisle Aas. |
|
28 use Digest::MD4; # Mike McCauley/Neil Winton/RSA Data Security |
|
29 use Digest::MD5 qw(md5 md5_hex md5_base64); # Copy-right 1998-2002 Gisle Aas. |
|
30 use Digest::SHA1 qw(sha1 sha1_hex sha1_base64); # Copy-right 1999-2001 Gisle Aas. |
|
31 |
|
32 #HMAC Functions |
|
33 use Digest::HMAC_MD2 qw(hmac_md2 hmac_md2_hex); |
|
34 use Digest::HMAC_MD4; |
|
35 use Digest::HMAC_MD5 qw(hmac_md5 hmac_md5_hex); |
|
36 use Digest::HMAC_SHA1 qw(hmac_sha1 hmac_sha1_hex); |
|
37 |
|
38 #-------------- MAIN SCRIPT ----------------- |
|
39 |
|
40 my $convfromhex = ''; |
|
41 GetOptions ('convfromhex' => \$convfromhex ); |
|
42 my $numArgs = $#ARGV + 1; |
|
43 |
|
44 my $file = @ARGV[0]; |
|
45 my $origkey = @ARGV[1]; |
|
46 my $key = $origkey; |
|
47 print "key is: $origkey\n"; |
|
48 if ( $convfromhex ) { |
|
49 $key = pack("H*", $origkey); |
|
50 print "Converting key from hex\n"; |
|
51 } |
|
52 |
|
53 #print("Data to be Hashed: \n"); |
|
54 |
|
55 my $data = ""; |
|
56 open(DATA, "< $file" ) or die "Can't open $file : $!"; |
|
57 binmode(DATA); |
|
58 |
|
59 while ( <DATA> ) { |
|
60 $data.= $_; |
|
61 } |
|
62 close(DATA); |
|
63 |
|
64 #print($data); |
|
65 #print("\n"); |
|
66 |
|
67 #----------Return Hashed Values---------- |
|
68 |
|
69 my $output = $file; |
|
70 |
|
71 substr($output,length($file)-4,length($file),"_HASHDATA.txt"); |
|
72 |
|
73 open(OUT, "> $output") or die "Can't open $output : $!"; |
|
74 |
|
75 print OUT "****** HASH MD2/MD4/MD5/SHA1 ******\n"; |
|
76 print OUT "\n"; |
|
77 |
|
78 print OUT "MD2: "; |
|
79 print OUT md2_hex($data); |
|
80 print OUT "\n"; |
|
81 |
|
82 print OUT "MD4: "; |
|
83 print OUT Digest::MD4->hexhash($data); |
|
84 print OUT "\n"; |
|
85 |
|
86 print OUT "MD5: "; |
|
87 print OUT md5_hex($data); |
|
88 print OUT "\n"; |
|
89 |
|
90 print OUT "SHA1: "; |
|
91 print OUT sha1_hex($data); |
|
92 print OUT "\n"; |
|
93 |
|
94 print OUT "\n"; |
|
95 print OUT "****** HMAC MD2/MD4/MD5/SHA1 ******\n"; |
|
96 print OUT "\n"; |
|
97 |
|
98 print OUT "HMAC-MD2: "; |
|
99 print OUT hmac_md2_hex($data, $key); |
|
100 print OUT "\n"; |
|
101 |
|
102 my $hmac = Digest::HMAC_MD4->new($key); |
|
103 $hmac->add($data); |
|
104 |
|
105 print OUT "HMAC-MD4: "; |
|
106 print OUT $hmac->hexdigest; |
|
107 print OUT "\n"; |
|
108 |
|
109 print OUT "HMAC-MD5: "; |
|
110 print OUT hmac_md5_hex($data, $key); |
|
111 print OUT "\n"; |
|
112 |
|
113 print OUT "HMAC-SHA1: "; |
|
114 print OUT hmac_sha1_hex($data, $key); |
|
115 print OUT "\n"; |
|
116 |
|
117 print OUT "\n"; |
|
118 |
|
119 print OUT "Key: "; |
|
120 print OUT $origkey; |
|
121 print OUT "\n"; |
|
122 |
|
123 close(OUT); |