Page 1 of 1

HOW TO Encode SHA256

PostPosted: Fri Apr 05, 2013 1:12 pm
by rainer
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

Re: HOW TO Encode SHA256

PostPosted: Fri Apr 05, 2013 4:43 pm
by david
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 2561 times


NCRYPT.js.zip
(5.72 KiB) Downloaded 115 times

Re: HOW TO Encode SHA256

PostPosted: Sat Apr 06, 2013 12:44 pm
by Roberto Blasco
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

Re: HOW TO Encode SHA256

PostPosted: Wed Jan 17, 2018 12:28 pm
by jdbruijn
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.

Re: HOW TO Encode SHA256

PostPosted: Wed Jan 17, 2018 2:24 pm
by jcompagner
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

Re: HOW TO Encode SHA256

PostPosted: Wed Jan 17, 2018 2:47 pm
by jdbruijn
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.

Re: HOW TO Encode SHA256

PostPosted: Thu Jan 18, 2018 12:36 am
by david
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).

Re: HOW TO Encode SHA256

PostPosted: Mon Jan 22, 2018 2:40 pm
by jcompagner
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.

Re: HOW TO Encode SHA256

PostPosted: Sat Feb 10, 2018 3:20 pm
by rainer
Btw.. i am jusing the following plug-in for years now...

http://www.mindfiresolutions.com/MFCryp ... lug-in.htm