md5 predicate

28 views
Skip to first unread message

Emanuele De Cupis

unread,
Oct 16, 2015, 10:56:40 AM10/16/15
to SWI-Prolog
Hi all,

I'm new in prolog and I have an unaswered question. 

I'm trying to write a md5 predicate that verifies the following:
    
    md5("my string", "my md5").

A truth instance of such predicate would be

    md5("long live and prosper", "bf1835ce984d2a97d31409394fe00e9a").

I looked into docs, and I found this: http://www.swi-prolog.org/pldoc/doc_for?object=crypt/2

    ?- phrase("$1$", E, _),
       crypt("My password", E),
       format('~s~n', [E]).


 I assume to create a clause similar to this:

    md5(P, M):-
       phrase("$1$", E, _),
       crypt(P, E),
       name(M, E),
       format('~s~n', [E]).

But this is actually the output    

    ?- md5("long live and prosper", "bf1835ce984d2a97d31409394fe00e9a").
       $1$AtnbRJvB$cZ4gZvG2Glelv8hfWztcY/
       false.
 

What am I getting wrong?


Thanks

*(Prolog implementation: swi-prolog on Mac OSX El Capitan)*

PS: this question appears on stackoverflow, too.

Jan Wielemaker

unread,
Oct 16, 2015, 11:07:13 AM10/16/15
to Emanuele De Cupis, SWI-Prolog
On 10/16/2015 04:56 PM, Emanuele De Cupis wrote:
> Hi all,
>
> I'm new in prolog and I have an unaswered question.
>
> I'm trying to write a /md5/ predicate that verifies the following:
> md5("my string", "my md5").
>
> A truth instance of such predicate would be
>
> md5("long live and prosper", "bf1835ce984d2a97d31409394fe00e9a").
>
> I looked into docs, and I found this:
> http://www.swi-prolog.org/pldoc/doc_for?object=crypt/2

This follows Unix MD5 password encryption (which is deprecated in
the meanwhile). It does a lot more then just MD5. Plain MD5 is
available from library(semweb/rdf_db):

?- use_module(library(semweb/rdf_db)).
?- rdf_atom_md5("long live and prosper", 1, X).
X = bf1835ce984d2a97d31409394fe00e9a.

Hopefully Julio will come with a nice binding to the SSL crypto
functions and you can do this and a lot more in a uniform way.
As is, various crypto primitives a are all over the place.

Cheers --- Jan

Emanuele De Cupis

unread,
Oct 16, 2015, 11:38:58 AM10/16/15
to SWI-Prolog, ema....@gmail.com
Hi Jan,

that's brilliant - i skipped it as I red "deprecated", shame on me.

However, why the following predicate fails?

rdf_atom_md5("long live and prosper", 1, "bf1835ce984d2a97d31409394fe00e9a").

Jan Wielemaker

unread,
Oct 17, 2015, 3:32:15 AM10/17/15
to Emanuele De Cupis, SWI-Prolog
On 10/16/2015 05:38 PM, Emanuele De Cupis wrote:
> Hi Jan,
>
> that's brilliant - i skipped it as I red "deprecated", shame on me.
>
> However, why the following predicate fails?
>
> rdf_atom_md5("long live and prosper", 1,
> "bf1835ce984d2a97d31409394fe00e9a").

?- "bf1835ce984d2a97d31409394fe00e9a" = 'bf1835ce984d2a97d31409394fe00e9a'.
false.

Typically predicates that need `text' in SWI-Prolog are flexible on the
input representation, but dictate a specific output and just unify the
result.

Cheers --- Jan

>
>
>
> Il giorno venerdì 16 ottobre 2015 17:07:13 UTC+2, Jan Wielemaker ha scritto:
>
> On 10/16/2015 04:56 PM, Emanuele De Cupis wrote:
> > Hi all,
> >
> > I'm new in prolog and I have an unaswered question.
> >
> > I'm trying to write a /md5/ predicate that verifies the following:
> > md5("my string", "my md5").
> >
> > A truth instance of such predicate would be
> >
> > md5("long live and prosper", "bf1835ce984d2a97d31409394fe00e9a").
> >
> > I looked into docs, and I found this:
> > http://www.swi-prolog.org/pldoc/doc_for?object=crypt/2
> <http://www.swi-prolog.org/pldoc/doc_for?object=crypt/2>
>
> This follows Unix MD5 password encryption (which is deprecated in
> the meanwhile). It does a lot more then just MD5. Plain MD5 is
> available from library(semweb/rdf_db):
>
> ?- use_module(library(semweb/rdf_db)).
> ?- rdf_atom_md5("long live and prosper", 1, X).
> X = bf1835ce984d2a97d31409394fe00e9a.
>
> Hopefully Julio will come with a nice binding to the SSL crypto
> functions and you can do this and a lot more in a uniform way.
> As is, various crypto primitives a are all over the place.
>
> Cheers --- Jan
>
> --
> You received this message because you are subscribed to the Google
> Groups "SWI-Prolog" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to swi-prolog+...@googlegroups.com
> <mailto:swi-prolog+...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/swi-prolog.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages