function [hash] = whirlpool(s,C,rc,vb)

% Computes the Whirlpool hash of the input string s. The hash is returned
% in a binary representation - as an 8-by-64 array of ones and zeros.
%
% Copyright (c) 2008 Karl J A Petre and Xavier J Ortiz.
% This file was last updated 7 December 2008.
%
% This code is included with the Matlab Whirlpool package.
%
% The Whirlpool algorithm was developed by:
%     Paulo S. L. M. Barreto (pbarreto@scopus.com.br)
%     Vincent Rijmen (vincent.rijmen@cryptomathic.com)
%
% This software is provided by the author as is and any express
% or implied warranties, including, but not limited to, the implied
% warranties of merchantability and fitness for a particular purpose
% are disclaimed. In no event shall the author or contributors be
% liable for any direct, indirect, incidental, special, exemplary, or
% consequential damages (including, but not limited to, procurement of
% substitute goods or services; loss of use, data, or profits; or
% business interruption) however caused and on any theory of liability,
% whether in contract, strict liability, or tort (including negligence
% or otherwise) arising in any way out of the use of this software,
% even if advised on the possibility of such damage.

if nargin < 2; bc = 1; else bc = 0; end % Assume constants must be built.
if nargin < 4; vb = 0; end % Assume no verbose output.

R = 10;
if bc
    [C,rc] = buildconstants(R);
end
slen = length(s);
blen = 32;
if slen+1 <= blen
    blen = 32;
else
    while blen <= slen+1
        blen = blen+64;
    end
end
blen = blen+32;
b = zeros(blen,8);
buffer = zeros(64,8);
hexs = dec2hex(s-0);
for i = 1:slen
    b(i,:) = hex2bv(hexs(i,:),8);
end
b(slen+1,:) = hex2bv('80',8); % Append a 1.
n = bv2hex(dec2bv(slen*8,256)); % Append the length of the source.
i = 0;
while i < length(n)
    b(blen-i/2,:) = hex2bv(n(length(n)-i-1:length(n)-i),8);
    i = i+2;
end
hash = zeros(8,64);
for h = 0:blen/64-1
    buffer = b((64*h+1:64*(h+1)),:);
    [hash] = processbuffer(C,rc,R,buffer,hash,vb);
    disp(['Source(' num2str(h) '):'])
    d = ['   '];
    for i = 0:3
        for j = 8*i:8*(i+1)-1
            d = [d bv2hex(buffer(j+1,:))];
        end
        d = [d ' '];
    end
    disp(d)
    d = ['   '];
    for i = 4:7
        for j = 8*i:8*(i+1)-1
            d = [d bv2hex(buffer(j+1,:))];
        end
        d = [d ' '];
    end
    disp(d)
    disp(['Hash(' num2str(h) '):'])
    disp(['   ' bv2hex(hash(1,:)) ' ' bv2hex(hash(2,:)) ' ' bv2hex(hash(3,:)) ' ' bv2hex(hash(4,:)) ])
    disp(['   ' bv2hex(hash(5,:)) ' ' bv2hex(hash(6,:)) ' ' bv2hex(hash(7,:)) ' ' bv2hex(hash(8,:)) ])
end