Starting slowly and questions...

Joe Armstrong joe@REDACTED
Tue Jul 6 10:54:13 CEST 2004


  I've encloded a little module I  use for this - it also computes MD5
checksums for  extremely large files using md5_update.  You might find
this useful.

Use like this:

     > {ok,B} = md5:file("week4.pdf").
     {ok,<<111,197,3,75,190,224,67,242,15,53,139,151,14,22,214,103>>}
    > md5:digest2str(B).
    "6fc5034bbee043f20f358b970e16d667"


Cheers

    /Joe

-module(md5).

%% author Joe Armstrong

-export([string/1, file/1, bin/1, digest2str/1]).

-define(BLOCKSIZE, 32768).
-define(IN(X,Min,Max), X >= Min, X =< Max).

%% md5:string(string())      -> BinDigest
%% md5:file(FileName)        -> {ok, BinDigest} | {error, E} 
%% md5:digest2str(BinDigest) -> StringDigest

%% md5:file works with chunks so should work correctly with extremely
%% large files

string(Str) -> digest2str(erlang:md5(Str)).

file(File) ->
    case file:open(File, [binary,raw,read]) of
	{ok, P} -> loop(P, erlang:md5_init());
	Error   -> Error
    end.

loop(P, C) ->
    case file:read(P, ?BLOCKSIZE) of
	{ok, Bin} ->
	    loop(P, erlang:md5_update(C, Bin));
	eof ->
	    file:close(P),
	    {ok, erlang:md5_final(C)}
    end.

digest2str(Digest) -> bin2str(binary_to_list(Digest)).

bin2str([H|T]) ->
    {H1, H2} = byte2hex(H),
    [H1,H2|bin2str(T)];
bin2str([]) ->
    [].

byte2hex(X) -> 
    {nibble2hex(X bsr 4), nibble2hex(X band 15)}.

nibble2hex(X) when ?IN(X, 0, 9)   -> X + $0;
nibble2hex(X) when ?IN(X, 10, 15) -> X - 10 + $a.

%% compute the md5 checksum of a binary
bin(Bin) ->
    C1 = erlang:md5_init(),
    C2 = erlang:md5_update(C1, Bin),
    C3 = erlang:md5_final(C2),
    digest2str(C3).

    














> I got the following to work:
> 
> File = "C:/c.txt".
> {_,Contents} = file:read_file(File).
> Digest = erlang:md5(Contents).
> 
> io:format("~.16b\n", [Digest]).
> 
> I tried various format options, binary_to_string and some bit stuff but
> clearly I am missing something.
> 
> I'd like to get it to output something like:
> f28c94b1
> 
> maybe there is a function to turn a arbitrary length binary into a hex
> string but
> 




More information about the erlang-questions mailing list