Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我如何像大多数网站和程序一样在android中生成SHA1?_Java_Android_Sha1_Password Protection_Password Encryption - Fatal编程技术网

Java 我如何像大多数网站和程序一样在android中生成SHA1?

Java 我如何像大多数网站和程序一样在android中生成SHA1?,java,android,sha1,password-protection,password-encryption,Java,Android,Sha1,Password Protection,Password Encryption,, -对于123的SHA-1,所有返回40BD001563085FC35165329EA1FF5C5ECBDBBEEF 但是,对于我的代码: public static String SHA1(String clearString) { try { MessageDigest messageDigest = MessageDigest.getInstance("SHA-1"); messageDigest.update(clearString.ge

,

-对于
123的SHA-1,所有返回
40BD001563085FC35165329EA1FF5C5ECBDBBEEF

但是,对于我的代码:

public static String SHA1(String clearString)
{
    try
    {
        MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
        messageDigest.update(clearString.getBytes("UTF-8"));
        byte[] bytes = messageDigest.digest();
        StringBuilder buffer = new StringBuilder();
        for (byte b : bytes)
        {
            buffer.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
        }
        return buffer.toString();
    }
    catch (Exception ignored)
    {
        ignored.printStackTrace();
        return null;
    }
}
和我尝试过的许多其他代码,我得到了一些不同的东西。通过上面的代码,我得到:
9c9598069a40434b500d862e1a13ab9d5a969fc8
for
123


看到所有网站都使用相同的算法,我似乎做错了什么

问题似乎出在您转换为
字符串的方式上

public static String byteArrayToString(byte[] bytes) {
    StringBuilder buffer = new StringBuilder();
    for (byte b : bytes) {
        buffer.append(String.format(Locale.getDefault(), "%02x", b));
    }
    return buffer.toString();
}

public static String SHA1(String clearString)
{
    try {
        MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
        messageDigest.update(clearString.getBytes("UTF-8"));
        return byteArrayToString(messageDigest.digest());
    } catch (Exception ignored) {
        ignored.printStackTrace();
        return null;
    }
}

因此
SHA1(“123”)
将导致:
40bd001563085fc35165329ea1ff5c5ecbdbbeef

看起来问题在于转换为
字符串的方式

public static String byteArrayToString(byte[] bytes) {
    StringBuilder buffer = new StringBuilder();
    for (byte b : bytes) {
        buffer.append(String.format(Locale.getDefault(), "%02x", b));
    }
    return buffer.toString();
}

public static String SHA1(String clearString)
{
    try {
        MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
        messageDigest.update(clearString.getBytes("UTF-8"));
        return byteArrayToString(messageDigest.digest());
    } catch (Exception ignored) {
        ignored.printStackTrace();
        return null;
    }
}

因此,
SHA1(“123”)
将导致:
40bd001563085fc35165329ea1ff5c5ecbdbbeef

试试。可能是@commonware的重复,但如果我想要一个字符串作为参数呢?@maszter您的评论指向的答案是0将被替换为零。这在我的例子中并没有发生。“但是如果我想要一个字符串作为参数呢?”--我不明白你的意思。话虽如此,j2ko的答案引起了我的关注:用其他东西替换
字节[]
-到十六进制的转换。顺便说一句,我真的希望您不要将SHA-1用于任何与安全相关的东西,因为它已经过时了。试试看。可能与@commonware重复,但如果我想要一个字符串作为参数呢?@maszter您的评论指向一个答案,即0将被替换为零。这在我的例子中并没有发生。“但是如果我想要一个字符串作为参数呢?”--我不明白你的意思。话虽如此,j2ko的答案引起了我的关注:用其他东西替换
字节[]
-到十六进制的转换。顺便说一句,我真的希望你不要用SHA-1做任何与安全相关的事情,因为它已经过时了。