HOW TO Encode SHA256

Questions and answers on designing your Servoy solutions, database modelling and other 'how do I do this' that don't fit in any of the other categories

HOW TO Encode SHA256

Postby rainer » Fri Apr 05, 2013 1:12 pm

I need to convert this line from php script to a working servoy (6.09) method...



$signature = base64_encode(hash_hmac("sha256", $string_to_sign, $private_key, true));

Any ideas ?


Thank you !

Rainer
rainer
 
Posts: 7
Joined: Fri Aug 17, 2012 9:58 am

Re: HOW TO Encode SHA256

Postby david » Fri Apr 05, 2013 4:43 pm

Create a new scope called "NCRYPT", unzip attached file, and paste file contents into the scope file. We've implemented a lot of functions and utilities—sample code for your case:

Code: Select all
var x = scopes.NCRYPT.HMAC(scopes.NCRYPT.SHA256,"Message","some key", { asBytes: true })
var y = scopes.NCRYPT.util.bytesToBase64(x)


This should cover you for now but note that we implemented with an older version of http://code.google.com/p/crypto-js/. Stay tuned for news about the latest version getting ported over (not by us!).

hmac.png
hmac.png (36.47 KiB) Viewed 1763 times


NCRYPT.js.zip
(5.72 KiB) Downloaded 86 times
David Workman, Kabootit

Image
Everything you need to build great apps with Servoy
User avatar
david
 
Posts: 1726
Joined: Thu Apr 24, 2003 4:18 pm
Location: Washington, D.C.

Re: HOW TO Encode SHA256

Postby Roberto Blasco » Sat Apr 06, 2013 12:44 pm

Thanks david for the script :-)

Hi, rainer. You can also encode SHA-256 with java classes.

Code: Select all
/**
* SHA-256 String encoding
*
* @param {String} string
* @return {String}
* @properties={typeid:24,uuid:"263E3A5F-0491-4F53-A2B8-1393EFE8C0BE"}
*/
function sha265(string){
   
   var  text = new Packages.java.lang.String(string);
   var md = Packages.java.security.MessageDigest.getInstance("SHA-256");
   md.update(text.getBytes("UTF-8"));
   
   /** @type {Array<byte>} */
   var bytes = md.digest();
   
   return Packages.org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString(bytes);
}


Best Regards. Roberto Blasco
Un saludo. Roberto.

Madrid - Spain
Tfno: (+34) 625653066
E-mail: [email protected]
User avatar
Roberto Blasco
007
 
Posts: 341
Joined: Tue Apr 08, 2008 7:18 pm
Location: Madrid / Spain

Re: HOW TO Encode SHA256

Postby jdbruijn » Wed Jan 17, 2018 12:28 pm

Hi,
I am looking for HMAC-MD5 hashing for integrating with an external application. However, when I try the HMAC function in NCRYPT, I get a different hash compared to when I try the same thing with the default PHP hash_hmac function. Is there maybe a newer version of this module available or do you have any idea on how I can get this to work?
I have taken a look at the crypto js file, to see if I can modify it to servoy code, but I do not know where to start.
Jos de Bruijn
Welcome CCS
Servoy Certified Developer
Image
jdbruijn
 
Posts: 465
Joined: Sun Apr 11, 2010 6:34 pm

Re: HOW TO Encode SHA256

Postby jcompagner » Wed Jan 17, 2018 2:24 pm

and you do use it with MD5 hasher?
I guess that can always be a bit different, maybe the implementation differs a bit, this is always a bit tricky, you really need to know exactly what PHP hash_hmac uses
Johan Compagner
Servoy
User avatar
jcompagner
 
Posts: 8086
Joined: Tue May 27, 2003 7:26 pm
Location: The Internet

Re: HOW TO Encode SHA256

Postby jdbruijn » Wed Jan 17, 2018 2:47 pm

Yes, using MD5 hasher. I have tried a sample where I use the cryptoJS function http://plnkr.co/edit/mSC7A0dTaHMf4DNwdfbu?p=preview
And compare it to the php hash-hmac and both values are equal. So the CryptoJS library should do what I need, but it looks like the implementation in the NCRYPT module is different as it does not give the same hash result.

I have also looked at the cryptojs used by the plunker to add it as a servoy scope. But I have no idea on where to start with that.
Jos de Bruijn
Welcome CCS
Servoy Certified Developer
Image
jdbruijn
 
Posts: 465
Joined: Sun Apr 11, 2010 6:34 pm

Re: HOW TO Encode SHA256

Postby david » Thu Jan 18, 2018 12:36 am

The version of the library we wrapped (http://code.google.com/p/crypto-js/) at the time was not the latest. (The successor to that library is https://github.com/digitalbazaar/forge.) This was due to it being relatively easy to convert over to a Servoy scope. We tried to to the latest version but:

1. Servoy doesn't support any of the JS modules implementations. Since most libraries are composed of modules, you end up having to mash everything together in one file to then put in a scope.

2. Most libraries nowadays are using a version of JS that Servoy doesn't support.

I actually messed around last year with Babel to transpile modern JS code into an output that Servoy might recognize thinking to solve both of the above challenges. And interesting experiment but you still have to convert that output into Servoy scopes format.

Summary: when you reach this edge of Servoy's capabilities it's likely better to drop into Java. Another intriguing option with Servoy 8 is use its websockets implementation to send chores over to other servers and get responses back quickly (can also just use REST or exec).
David Workman, Kabootit

Image
Everything you need to build great apps with Servoy
User avatar
david
 
Posts: 1726
Joined: Thu Apr 24, 2003 4:18 pm
Location: Washington, D.C.

Re: HOW TO Encode SHA256

Postby jcompagner » Mon Jan 22, 2018 2:40 pm

i quickly looked at: https://github.com/digitalbazaar/forge

but as far as i really can see (i looked through a few js files) those all should run just fine on Rhino
I don't really see ECMA6 features (but i could be wrong or overlooked something)

The problem is that they write against nodejs or at least they expect a module system (require()/modules.xxxx)
So for this to work in servoy you have to use something like CommonJS
it seems like a new rhino would have some support for this: https://developer.mozilla.org/en-US/doc ... hino_1.7R3

for 8.3 we are upgrading to the lastest version of rhino, but not sure if that works out of the box.
Johan Compagner
Servoy
User avatar
jcompagner
 
Posts: 8086
Joined: Tue May 27, 2003 7:26 pm
Location: The Internet

Re: HOW TO Encode SHA256

Postby rainer » Sat Feb 10, 2018 3:20 pm

Btw.. i am jusing the following plug-in for years now...

http://www.mindfiresolutions.com/MFCryp ... lug-in.htm
rainer
 
Posts: 7
Joined: Fri Aug 17, 2012 9:58 am


Return to Programming with Servoy

Who is online

Users browsing this forum: No registered users and 6 guests

cron