PointyCastle / pointycastle

Moved into the Bouncy Castle project: https://github.com/bcgit/pc-dart
MIT License
271 stars 75 forks source link

Simple Example(s) #159

Open bagintz opened 6 years ago

bagintz commented 6 years ago

I am hoping to use this library since I have to integrate an api that uses Hmac/SHA-512 and the base crypto library for Dart doesn't support that (?????). I am having a hard time finding an end to end example of how to emulate the api documentation. Can someone point me to a place with an example?

Thanks!!!

bagintz commented 6 years ago

I have found a couple of things but I am having a hard time getting the same result as my working examples in Postman. Does anybody have any examples?

bagintz commented 6 years ago

Specifically trying to duplicate this (JS) in PointyCastle, and I cannot get it :(

CryptoJS.enc.Hex.stringify(CryptoJS.HmacSHA512(MESSAGE, KEY));

bagintz commented 6 years ago

Sigh, ok, at the end of the day I think my lack of understanding led to, what I am sure, is a very easy concept to understand. Essentially I was using a block size of 64 (which seems to be the default of the SHA-512 algo, but it seems that it actually wants 128 to match the output of the JS above.

So for whatever person runs into the same issue here is a very concise example of how to do a SHA-512 HMAC using pointycastle

Uint8List hmacSHA512(Uint8List key,Uint8List data) {
  final _tmp = new HMac(new SHA512Digest(), 128)..init(new KeyParameter(key));
  return _tmp.process(data);
}

String formatBytesAsHexString(Uint8List bytes) {
  var result = new StringBuffer();
  for( var i=0 ; i<bytes.lengthInBytes ; i++ ) {
    var part = bytes[i];
    result.write('${part < 16 ? '0' : ''}${part.toRadixString(16)}');
  }
  return result.toString();
}

Uint8List createUint8ListFromString( String s ) {
  var ret = new Uint8List(s.length);
  for( var i=0 ; i<s.length ; i++ ) {
    ret[i] = s.codeUnitAt(i);
  }
  return ret;
}
Uint8List message = createUint8ListFromString("12345");
Uint8List key = createUint8ListFromString("test");
String digest = formatBytesAsHexString(hmacSHA512(message, key)));

I found the hmacSHA512() method here: https://github.com/anicdh/bip32-dart

stevenroose commented 6 years ago

You could use the algorithm registry and just done new Mac("HMAC/SHA-512"). And use the hex package for hexadecimal encoding and decoding.

bagintz commented 5 years ago

Cool, I will give that a shot, thank you

sakinaboriwala commented 5 years ago

@stevenroose @bagintz I am struggling with this. Cannot find any example in ReadMe or anywhere else in order to implement SHA-512 encryption. It's really urgent, please help me out if possible.

Need to implement something like this const encryptedHash = CryptoJS.SHA512(paramsList); This is a JS snippet.

stevenroose commented 5 years ago

I'm not longer able to actively maintain this package. All I can do is look at PRs and merge them if I have the time to verify them or if they have been confirmed working by other contributors.

Every once and a while someone comes by and complaints about lack of example code. I sometimes give them some pointers and invite them to contribute their examples into the repo. I never hear again from them.

I think I'm finally gonna put up a message on top of the README that it's no longer maintained.

JiananYe commented 5 years ago

String digest = formatBytesAsHexString(hmacSHA512(message, key)));

you twisted the message and key parameter for correct results