Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 8 of 8 total
Thread Problem use library crypto-js
Wed, Jul 22 2015 10:45 AMPermanent Link

Pasquale

Web Pos srl

Hi I would like to use the library MD5

https://code.google.com/p/crypto-js/

in my application but I can not ( I get the error : MD5 is not defined)

Someone can help ?

I Occor a practical example of using the function MD5 , tanks .
Wed, Jul 22 2015 2:41 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Pasquale,

<< in my application but I can not ( I get the error : MD5 is not defined)
>>

Did you define an external interface in EWB for the external JS code ?

http://www.elevatesoft.com/manual?action=viewtopic&id=ewb2&topic=External_Interfaces

If not, then you'll need that first.

Tim Young
Elevate Software
www.elevatesoft.com
Wed, Jul 22 2015 2:43 PMPermanent Link

Raul

Team Elevate Team Elevate

On 7/22/2015 10:45 AM, Pasquale wrote:
> Hi I would like to use the library MD5
> https://code.google.com/p/crypto-js/
> in my application but I can not ( I get the error : MD5 is not defined)
> Someone can help ?

You need to use external interfaces if you wish to use external library:

http://www.elevatesoft.com/manual?action=viewtopic&id=ewb2&topic=External_Interfaces

In general you need to do 2 things:
1a. include the external javascript file in the project (this way EWB
takes care of loading it etc).
OR
1b. if you want to access the file by URL then you need to use the
TScript control to load it runtime (assing the URL to the TScript URL
property)

2. declare the functions in the file using the "external" keyword (i.e.
"external function MD5(sIn:string):string;")

3. now you can call MD5 from your code

For a generic sample on this see for example this thread (and there are
others):

http://www.elevatesoft.com/forums?action=view&category=ewb&id=ewb2_preview&page=1&msg=976#976


Raul
Sun, Nov 15 2015 12:37 PMPermanent Link

Tom Urlep

/*
Raul wrote:

On 7/22/2015 10:45 AM, Pasquale wrote:
> Hi I would like to use the library MD5
> https://code.google.com/p/crypto-js/
> in my application but I can not ( I get the error : MD5 is not defined)
> Someone can help ?

You need to use external interfaces if you wish to use external library:

http://www.elevatesoft.com/manual?action=viewtopic&id=ewb2&topic=External_Interfaces

In general you need to do 2 things:
1a. include the external javascript file in the project (this way EWB
takes care of loading it etc).
OR
1b. if you want to access the file by URL then you need to use the
TScript control to load it runtime (assing the URL to the TScript URL
property)

2. declare the functions in the file using the "external" keyword (i.e.
"external function MD5(sIn:string):string;")

3. now you can call MD5 from your code

For a generic sample on this see for example this thread (and there are
others):

http://www.elevatesoft.com/forums?action=view&category=ewb&id=ewb2_preview&page=1&msg=976#976


Raul
*/

Attached is an example of calling function MD5 from CryptoJS according to
advice in this post. Can somebody enlighten me what is wrong?



Attachments: HashTest.7z
Sun, Nov 15 2015 12:51 PMPermanent Link

Tom Urlep

Tom Urlep wrote:

/*
Raul wrote:

On 7/22/2015 10:45 AM, Pasquale wrote:
> Hi I would like to use the library MD5
> https://code.google.com/p/crypto-js/
> in my application but I can not ( I get the error : MD5 is not defined)
> Someone can help ?

You need to use external interfaces if you wish to use external library:

http://www.elevatesoft.com/manual?action=viewtopic&id=ewb2&topic=External_Interfaces

In general you need to do 2 things:
1a. include the external javascript file in the project (this way EWB
takes care of loading it etc).
OR
1b. if you want to access the file by URL then you need to use the
TScript control to load it runtime (assing the URL to the TScript URL
property)

2. declare the functions in the file using the "external" keyword (i.e.
"external function MD5(sIn:string):string;")

3. now you can call MD5 from your code

For a generic sample on this see for example this thread (and there are
others):

http://www.elevatesoft.com/forums?action=view&category=ewb&id=ewb2_preview&page=1&msg=976#976


Raul
*/

Attached is an example of calling function MD5 from CryptoJS according to
advice in this post. Can somebody enlighten me what is wrong?

Previous attachment was incomplete. Sorry



Attachments: HashTest.zip
Sun, Nov 15 2015 4:41 PMPermanent Link

Raul

Team Elevate Team Elevate

On 11/15/2015 12:51 PM, Tom Urlep wrote:
> Attached is an example of calling function MD5 from CryptoJS according to
> advice in this post. Can somebody enlighten me what is wrong?

There is no function "md5" in the javascript library you're loading (
http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/md5.js ).

External function declarations must match "exactly" to the actual
functions - "external" just tells EWB at compile time "don't worry and
assume this function call is ok" and then when you call it you get a
runtime error since it does not exist.

Looking at the CryptoJS it is PITA to read (at least for my JS skills)
so i cheated for now and for quick solution just wrote a wrapper for it.

Anyways, create a new javascript file which consists of this:

function myMD5(s)
{
   var hash = CryptoJS.MD5(s);
   return hash.toString();
}


Then save it to a file and include as external file in EWB project (for
example helper.js)

Change your external definition to

"external function myMD5(s:string):string;"

and then call to it as such

"Label1.Caption := myMD5( Edit1.Text);"


It works for me here - i'm seeing proper MD5 hashes generated - i will
just comment that one should not use MD5 for anything these days.

Would need Tim or somebody who deals with external JS more to see if
there is a more straightforward way of dealing with this.

Raul
Mon, Nov 16 2015 3:35 AMPermanent Link

Tom Urlep

/*
Anyways, create a new javascript file which consists of this:

function myMD5(s)
{
   var hash = CryptoJS.MD5(s);
   return hash.toString();
}


Then save it to a file and include as external file in EWB project (for
example helper.js)

Change your external definition to

"external function myMD5(s:string):string;"

and then call to it as such

"Label1.Caption := myMD5( Edit1.Text);"
*/

I tried your advice and it works OK. Thank you.

Tom
Mon, Nov 16 2015 2:34 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Raul,

<< Looking at the CryptoJS it is PITA to read (at least for my JS skills)  >>

Yeah, it's a bit of a mess, structurally, but pretty much par for the course for JS libraries.  Use JS - you can design your own inheritance model ! Wink

The key phrase is in the docs:

"The hash you get back isn't a string yet. It's a WordArray object. When you use a WordArray object in a string context, it's automatically converted to a hex string."

So, the hash result is a WordArray, so you would need to define the minimum external interface like this:

type

  external TWordArray emit CryptoJS.WordArray = class
     public
        { Encoder parameter not prototyped here... }
        function toString: String;
        function concat(sourceArray: TWordArray): TWordArray;
        procedure clamp;
        function clone: TWordArray;
        class function random(numBytes: Integer): TWordArray;
     end;

  external CryptoJS emit CryptoJS = class
     public
        class function MD5(const input: String): TWordArray;
     end;

and then use it like this:

procedure TForm1.Button2Click(Sender: TObject);
begin
  ShowMessage(CryptoJS.MD5('Password').toString);
end;

I just loaded the JS dynamically using a TScript instance.

Tim Young
Elevate Software
www.elevatesoft.com
Image