I want to get sha2 hash of particular java object. I don't want it to be int
, I want byte[]
or at least String
. I've got the following code to create sha2:
static byte[] sha2(byte[] message) { if (message == null || message.length == 0) { throw new IllegalArgumentException("message is null"); } try { MessageDigest sha256 = MessageDigest.getInstance(SHA_256); return sha256.digest(message); } catch (NoSuchAlgorithmException e) { throw new IllegalArgumentException(e); }}
I can just convert my object to byte[]
, but I don't think it's a good idea to store big array in memory just to create 32 byte array. So how can I compute sha2(or maybe another crypto hash function) of object?
I need to write some code to work in bash that emulates the HMAC generator found here:
https://www.freeformatter.com/hmac-generator.html#ad-output
So far, I have this (NOTE: these are not real passwords or salt values so I have no problems using them here. The 'key.txt' file just contains the 'key' variable value):
key="123456781000000003PASSWORDLOGIN"salt1="99992423A9495A45-8826-44AC-999C-219EFB13EB1A99992423"echo -n "$salt1" | xxd -p -c 256 > hex1.hexhexKey=`cat hex1.hex`openssl dgst -sha256 -mac HMAC -macopt hexkey:$hexKey key.txt
My output is this:
fb1133ec9066ad3b5a487893febd2b2d803914ebac2ee6e6a9c587d7219cda65
...but on that freeformatter site, I get this:
e366c0cbd2072c5739626b3e0a46728928df740dbfeb68d4deeaa525147ced61
Any ideas what I am doing wrong?
I am following this tutorial to sign data with SHA-256withECDSA.
Using the same private key and message, it generated different signature result every time. The only change I made is return Base64 encoded string instead of Base58
Is it supposed to be the same signature every time?
public static string GetSignature(string privateKey, string message){ var curve = SecNamedCurves.GetByName("secp256k1"); var domain = new ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H); var keyParameters = new BC.Crypto.Parameters.ECPrivateKeyParameters(new BC.Math.BigInteger(privateKey), domain); ISigner signer = SignerUtilities.GetSigner("SHA-256withECDSA"); signer.Init(true, keyParameters); signer.BlockUpdate(Encoding.ASCII.GetBytes(message), 0, Encoding.ASCII.GetBytes(message).Length); var signature = signer.GenerateSignature(); return Convert.ToBase64String(signature);}
Example result
MEQCIB0rqb8Dbrh+e2akoCVJaUS4tyJYqfRf8vdz/W2fUOomAiB3D2BaMYjwSgKRQyTd/W+YEn+wT0I4dq1hmgBfe/Sh7g==MEUCIQDsWxG8Zr7MCemgGylAN+Y32qJYuDmqZMpaPwxTKosJ3AIgE3oSsBjcua/aCvfNXiMfcUM9U92p9aRlAIEopw/wvd0=MEYCIQCjQ0EDHVFhASuUSPnCGjCb0O1sq3Op+aAl01afjIVviQIhAOnGyGN9cKswFn97de0o/Im9Hswo6AdnLhKIZSUcYbDYMEYCIQCAqcHyhRcbLtuyimJ4XCHvJcz0p0Wd7FgJ1+07sOsC/gIhAKYwlhRv98C/3XeZE1TujkB9qMn2C99GaguJoWng9+2yMEUCIQD7ObA0n0JpRNQDe+3udpeKGEk79KsrjHsjv/4Wlj2bigIgZERRSQBEN91HTJHqn+prlwSCKUT4AJx061Gi0tv8Xuw=
I'm trying to implement JWT token (encoding only) in WebAssembly, the goal is to have a very light weight wasm module. As a web developer my C knowledge is limited. For now I've implemented the following function (ported from JS) to encode url-safe Base64 encoder, which works perfectly.
char _keyStr[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=";char ret_val[200];char* encode (char *data){ int len = strlen(data); int i = 0; int j = 0; while(i<len){ char chr1 = data[i++]; int chr2Out = (i > len - 1)? 1:0; char chr2 = data[i++]; int chr3Out = (i > len - 1)? 1:0;; char chr3 = data[i++]; char enc1 = chr1 >> 2; char enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); char enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); char enc4 = chr3 & 63; if (chr2Out) { enc3 = enc4 = 64; } else if (chr3Out) { enc4 = 64; } ret_val[j++] = _keyStr[enc1]; ret_val[j++] = _keyStr[enc2]; ret_val[j++] = _keyStr[enc3]; ret_val[j++] = _keyStr[enc4]; } ret_val[j] = '\0'; return ret_val;}
My next challenge is to be able to sign my JWT payload with HmacSHA256.The following JS fiddle, describes what I want to accomplish with C.https://jsfiddle.net/gm7boy2p/813/
I'm struggling with integrating a 3rd party code and complie it with emcc.I'm looking for a light weight library or a snippet.
Example code or any help would be appreciated.
Update: After extra research, reading this stackoverflow question and this article, it looks like using openssl or any other external library with WebAssembly is far from trivial. So what I'm looking for now is a standalone C function that I could integrate to my existing code.
I have some string and I want to hash it with the SHA-256 hash function using C#. I want something like this:
string hashString = sha256_hash("samplestring");
Is there something built into the framework to do this?
Дом - Мапа сајта - Приватност - Линкови - Цопиригхт © 2018 Цортек ИТ доо : Контакт : admin @ cortexit.co.uk
Please note that by viewing our site you agree to our use of cookies (see Приватност for details). You will only see this message once.