digest program problem
Show older comments
i use the following program to calculate a message digest for a message but i get error
??? Error: Expected a variable, function, or constant, found "incomplete string".
the program
%function hm = createdigest(m, varargin);
%A function which takes message string, m and creates a message digest
%using SHA-1, as described in federal publication FIPS 180-2.
%If the message is anything other than a binary string, use the 'convert'
%string. e.g. sha = createdigest('abc', 'convert')
%If dealing with binary strings, just call the script like this:
%
% sha = createdigest('011000010110001001100011');msgbox({'any random number must be less than',e},'take care');
x=inputdlg({'ENTER THE MESSAGE:'},'enter your message',1);
m=char(x);
maple('m:=',m);
%Initial Hash Values
%Notation: H[word number](block number) in a 32-bit binary string
H00=dec2bin(hex2dec('67452301'),32);
H10=dec2bin(hex2dec('efcdab89'),32);
H20=dec2bin(hex2dec('98badcfe'),32);
H30=dec2bin(hex2dec('10325476'),32);
H40=dec2bin(hex2dec('c3d2e1f0'),32);
%Preprocessing
%1) Padding the message
%convert message from a message string to a binary string
s = '';
if strcmp('convert', 'varargin')
for a = 1:length(m),
binstr = dec2bin(double(m(a)), 8);
s=[s, binstr];
end
else
s = m;
end
%Pad binary string to be a multiple of 512 bits.
l = dec2bin(length(s),8);
lstring = [int2str(zeros(64-length(l),1))' l];
s = [s, '1', int2str(zeros(mod(448-length(s)-1,512),1))', lstring];
%2) Parsing the padded message
%Padded message is subdivided into N 512-bit blocks
for n = 1:(length(s)/512),
eval(['M' int2str(n) '= s(512*(n-1)+1:512*n) ;']);
end
%Actual Hash Computation
for n = 1:(length(s)/512),
for t = 1:16,
%First 15 32-bit words of the message schedule
eval(['Wlog' sprintf('%d', (t-1)) ' = transpose(str2num(transpose(M' sprintf('%d', n) '(32*(t-1)+1:32*t))));']);
eval(['W' sprintf('%d',(t-1)) ' = bin2dec(M' sprintf('%d', n) '(32*(t-1)+1:32*t));'])
end
for t = 16:79,
eval(['Wlog' sprintf('%d',t) ' = transpose(circshift(transpose(bitxor(bitxor(bitxor(''Wlog' sprintf('%d',t-3) ',Wlog' sprintf('%d',t-8) '),Wlog' sprintf('%d',t-14) '),Wlog' sprintf('%d',t-16) ')), -1)) ;'])
eval(['W' sprintf('%d',t) ' = bin2dec(transpose(int2str(transpose(Wlog' sprintf('%d', t) '))));'])
end
%Initializing the five working string variables
eval(['a=logical(transpose(str2num(transpose(H0' sprintf('%d', n-1) '))));']);
eval(['b=logical(transpose(str2num(transpose(H1' sprintf('%d', n-1) '))));']);
eval(['c=logical(transpose(str2num(transpose(H2' sprintf('%d', n-1) '))));']);
eval(['d=logical(transpose(str2num(transpose(H3' sprintf('%d', n-1) '))));']);
eval(['e=logical(transpose(str2num(transpose(H4' sprintf('%d', n-1) '))));']);
for t = 0:79,
%Setting up the schedule for function f and variable K
if (t>=0 & t<=19), f = xor(and(b,c),and(not(b),d)); K=hex2dec('5a827999');
elseif (t>=20 & t<=39), f = xor(xor(b,c),d); K=hex2dec('6ed9eba1');
elseif (t>=40 & t<=59), f = xor(xor(and(b,c),and(b,d)),and(c,d)); K=hex2dec('8f1bbcdc');
elseif (t>=60 & t<=79), f = xor(xor(b,c),d); K=hex2dec('ca62c1d6');
end
%Shifting the working variables according to the schedule
T=sprintf('%d', [circshift(a(:),-5)';f;e]);
T=bin2dec(reshape(T, 3, 32));
T=mod(T(1)+T(2)+T(3)+K, 2^32);
eval(['T = mod(T + ' 'W' sprintf('%d', t) ', 2^32);']);
T = logical(str2num(dec2bin(T, 32)')');
e = d;
d = c;
c = circshift(b(:), -30)';
b = a;
a = T;
%debug line
%disp(t), disp(dec2hex(bin2dec(int2str(a')'),8)), disp( dec2hex(bin2dec(int2str(b')'),8)), disp( dec2hex(bin2dec(int2str(c')'),8)), disp(dec2hex(bin2dec(int2str(d')'),8)), disp( dec2hex(bin2dec(int2str(e')'),8))
end
%Compute the immediate hash value
eval(['H0' int2str(n) ' = dec2bin(mod(bin2dec(transpose(int2str(transpose(a)))) + bin2dec(num2str(H0' sprintf('%d', n-1) ')), 2^32), 32);' ]);
eval(['H1' int2str(n) ' = dec2bin(mod(bin2dec(transpose(int2str(transpose(b)))) + bin2dec(num2str(H1' sprintf('%d', n-1) ')), 2^32), 32);' ]);
eval(['H2' int2str(n) ' = dec2bin(mod(bin2dec(transpose(int2str(transpose(c)))) + bin2dec(num2str(H2' sprintf('%d', n-1) ')), 2^32), 32);' ]);
eval(['H3' int2str(n) ' = dec2bin(mod(bin2dec(transpose(int2str(transpose(d)))) + bin2dec(num2str(H3' sprintf('%d', n-1) ')), 2^32), 32);' ]);
eval(['H4' int2str(n) ' = dec2bin(mod(bin2dec(transpose(int2str(transpose(e)))) + bin2dec(num2str(H4' sprintf('%d', n-1) ')), 2^32), 32);' ]);
end
blocknum=int2str(length(s)/512);
eval(['sha = [H0' blocknum ' H1' blocknum ' H2' blocknum ' H3' blocknum ' H4' blocknum '];' ]);
hm = maple('convert', sha, 'decimal', 'binary');
%convert to hexadecimal for testing purposes
% H01 = dec2hex(bin2dec(H01))
% H11 = dec2hex(bin2dec(H11))
% H21 = dec2hex(bin2dec(H21))
% H31 = dec2hex(bin2dec(H31))
% H41 = dec2hex(bin2dec(H41))
%useful debug commands
%dec2hex(bin2dec(int2str(W15')'),8)
2 Comments
Image Analyst
on 17 Feb 2012
Note: this code requires maple. (I copied, pasted, and ran it (despite the messed up formatting) and it complained about me not having maple.
Walter Roberson
on 18 Feb 2012
What a mess; what a senseless waste of eval()'s.
How about a clue about which line it complains about?
There is a line that looks problematic to me:
T = logical(str2num(dec2bin(T, 32)')');
This could potentially mean to use a quoted parenthesis rather than a conjugate transpose, end the argument, conjugate transpose the result of that expression.
Answers (0)
Categories
Find more on Characters and Strings in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!