Checksum function in PL/SQL


This is a port of a C function that did a 32bit, 2's complement checksum.

function checksum( p_buff in varchar2 ) return number
is
    l_sum   number default 0;
    l_n     number;
begin
    for i in 1 .. trunc(length(p_buff||'x')/2) loop
        l_n := ascii(substr(p_buff||'x', 1+(i-1)*2, 1))*256 +
               ascii(substr(p_buff||'x', 2+(i-1)*2, 1));
        l_sum := mod(l_sum+l_n,4294967296);
    end loop;
    while ( l_sum > 65536 ) loop
        l_sum := bitand( l_sum, 65535 ) + trunc(l_sum/65536);
    end loop;
    return l_sum;
end checksum;