generate SHA1 hash function in Codename One

二次信任 提交于 2019-12-07 19:34:27

问题


I need to generate a hash function in CN1. I added the BouncyCastle library, but I don't know how to use it. After searching a lot, I found some examples and came up with this:

@Override
protected void onMain_ButtonSHA1Action(Component c, ActionEvent event) {
    String data = "XXXXXXXXXXXXX";
    SHA1Digest sha1 = new SHA1Digest();
    try {
        byte[] b = data.getBytes("UTF-8");
        sha1.update(b, 0, b.length);
        byte[] hash = new byte[sha1.getDigestSize()];
        sha1.doFinal(hash, 0);

        labelX.setText(hash.toString());

    } catch (Exception ex) {
    }

}

The result is not working. I know, because I am checking the outcome here: http://www.sha1-online.com/ and it is not the same.


回答1:


You are calling toString on a byte[] use new String(hash, "UTF-8").



来源:https://stackoverflow.com/questions/37042161/generate-sha1-hash-function-in-codename-one

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!