Skip to content

HamcSHA224 encryption error #235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
hujuny opened this issue Aug 20, 2021 · 4 comments
Closed

HamcSHA224 encryption error #235

hujuny opened this issue Aug 20, 2021 · 4 comments
Labels
package:crypto type-question A question about expected behavior or functionality

Comments

@hujuny
Copy link

hujuny commented Aug 20, 2021

When I used HamcSHA224, I found that the results of encrypted and online encrypted websites were different. I found many encrypted websites to compare, and they were all the same.
crypto: ^2.1.4

Digest hamcEncryption({@required String key,@required String content, @required Hash hash}) { var bytes = utf8.encode(content); var keyString=utf8.encode(key); var hmac = Hmac(hash, keyString); var digest = hmac.convert(bytes); return digest; }
print("${hamcEncryption(key: "1234", content: "foo", hash: sha224)}");

@natebosch
Copy link
Member

natebosch commented Aug 27, 2021

It works for me:

import 'dart:convert';

import 'package:crypto/crypto.dart' show Hmac, sha224;

void main() {
  var key = utf8.encode('some key');
  var content = utf8.encode('some content');
  print('Key: ${base64.encode(key)}');
  print('Content: ${base64.encode(content)}');
  var hmac= Hmac(sha224,key);
  var result = hmac.convert(content);
  print(result);
}

Prints:

Key: c29tZSBrZXk=
Content: c29tZSBjb250ZW50
6a9fd425734f37c5045a27ea9533b02dec9d93358b52d23b262f7695

When I plug those base64 encoded values into https://www.liavaag.org/English/SHA-Generator/HMAC/ I get

6a9fd425734f37c5045a27ea9533b02dec9d93358b52d23b262f7695

@natebosch
Copy link
Member

If I downgrade to 2.1.4 and adapt your example I also see a result that agrees with https://www.liavaag.org/English/SHA-Generator/HMAC/

import 'dart:convert';

import 'package:crypto/crypto.dart' show Hmac, sha224, Digest, Hash;

Digest hamcEncryption({String key, String content, Hash hash}) {
  var bytes = utf8.encode(content);
  var keyString = utf8.encode(key);
  var hmac = Hmac(hash, keyString);
  var digest = hmac.convert(bytes);
  return digest;
}

void main() {
  print(base64.encode(utf8.encode('1234')));
  print(base64.encode(utf8.encode('foo')));
  print("${hamcEncryption(key: "1234", content: "foo", hash: sha224)}");
}

prints

MTIzNA==
Zm9v
7bc4fa1501e9864400fe802b89fe71edda20104fa354232fce2d173f

Plugging those base64 values into the site results in the same digest as this library. Perhaps you did not input the same data in your tests.

@natebosch natebosch added the type-question A question about expected behavior or functionality label Aug 27, 2021
@hujuny
Copy link
Author

hujuny commented Aug 27, 2021

这个对我有用:

导入 “飞镖:转换”;

导入 'package:crypto/crypto.dart' 显示Hmac,sha224;

void  main () {
   var key = utf8. 编码('一些密钥');
  var内容= utf8。编码('一些内容');
  打印('键:$ { BASE64。编码(钥匙)} ');
  打印('内容:${ base64。编码(内容)} ');
  var hmac =  Hmac (sha224,key);
  无功结果= hmac。转换(内容);
  打印(结果);
}

印刷:

@hujuny
Copy link
Author

hujuny commented Aug 27, 2021 via email

@mosuem mosuem transferred this issue from dart-archive/crypto Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package:crypto type-question A question about expected behavior or functionality
Projects
None yet
Development

No branches or pull requests

3 participants