本文介绍了在Codename One中生成SHA1哈希函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在CN1中生成一个哈希函数。我添加了BouncyCastle库,但我不知道如何使用它。经过大量搜索后,我发现了一些示例并提出了以下建议:

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) {
    }

}

结果不起作用。我知道,因为我在这里检查结果: http://www.sha1-online.com/

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.

推荐答案

您正在呼叫 toString byte [] 上使用 new String(hash, UTF-8)

这篇关于在Codename One中生成SHA1哈希函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 03:43