在Android上做SHA1加密?[英] Make SHA1 encryption on Android?

本文是小编为大家收集整理的关于在Android上做SHA1加密?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

您可以建议我使用SHA1算法加密字符串吗? 我已经搜索了它.但是没有运气.

预先感谢.

推荐答案

binnyb的convertToHex方法无法正常工作.对我有用的更正确的是:

private static String convertToHex(byte[] data) { 
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < data.length; i++) { 
        int halfbyte = (data[i] >>> 4) & 0x0F;
        int two_halfs = 0;
        do { 
            if ((0 <= halfbyte) && (halfbyte <= 9)) {
                buf.append((char) ('0' + halfbyte));
            }
            else {
                buf.append((char) ('a' + (halfbyte - 10)));
            }
            halfbyte = data[i] & 0x0F;
        } while(two_halfs++ < 1);
    } 
    return buf.toString();
} 


public static String SHA1(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException  { 
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    byte[] sha1hash = new byte[40];
    md.update(text.getBytes("iso-8859-1"), 0, text.length());
    sha1hash = md.digest();
    return convertToHex(sha1hash);
} 

使用SHA1方法获取您的SHA1字符串.

更新:提供完整的答案

其他推荐答案

这是我在搜索SHA1算法实现时发现的2种方法:

private static String convertToHex(byte[] data) { 
    StringBuffer buf = new StringBuffer();
    int length = data.length;
    for(int i = 0; i < length; ++i) { 
        int halfbyte = (data[i] >>> 4) & 0x0F;
        int two_halfs = 0;
        do { 
            if((0 <= halfbyte) && (halfbyte <= 9)) 
                buf.append((char) ('0' + halfbyte));
            else 
                buf.append((char) ('a' + (halfbyte - 10)));
            halfbyte = data[i] & 0x0F;
        }
        while(++two_halfs < 1);
    } 
    return buf.toString();
}

public static String SHA1(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException  { 
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    byte[] sha1hash = new byte[40];
    md.update(text.getBytes("iso-8859-1"), 0, text.length());
    sha1hash = md.digest();
    return convertToHex(sha1hash);
} 

使用SHA1方法获取您的SHA1字符串.我尚未确认这确实是SHA1,但它适用于我的应用程序.

其他推荐答案

我之前已经回答了(如何在Android中hash hash?)但是在这里也很合适:

Android随附Apache的Commons编解码器,因此您可以简单地使用以下行创建SHA-1 HEXED字符串:

String myHexHash = DigestUtils.shaHex(myFancyInput);

这是您默认使用Android 4获得的旧弃用方法.新版本的摘要带来了sha256hex()等shahex()方法的所有口味,并与不同的参数类型过载.

当然,消化和Commons的其余编解码器中有更多功能.只是看看.

http://commons.apache.org/proper/commons-codec//javadocs/api-release/org/opache/commons/commons/commons/commons/codec/digestils.htmls.html

编辑:

如果您获得了classNotFoundError,则必须明确地将Commons-Codec作为依赖项添加(即使它应该与Android一起作为传递依赖性),例如:

    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.7</version>
    </dependency>

,您还必须将呼叫更改为:

String myHexHash = new String(Hex.encodeHex(DigestUtils.sha512(myFancyInput)));

(我谦虚的猜测是,这可能是由于Android VM中的classloader问题(class name Collision) - 实际上证明了Commons -Codec类已经存在...)

另请参见: https://stackoverflow.com/a/9284092/621690

本文地址:https://www.itbaoku.cn/post/102423.html

问题描述

Can you suggest me about how to encrypt string using SHA1 algorithm ? I've searched about it. But no luck.

Thanks in advance.

推荐答案

binnyb's convertToHex method is not working properly. A more correct one that works for me is:

private static String convertToHex(byte[] data) { 
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < data.length; i++) { 
        int halfbyte = (data[i] >>> 4) & 0x0F;
        int two_halfs = 0;
        do { 
            if ((0 <= halfbyte) && (halfbyte <= 9)) {
                buf.append((char) ('0' + halfbyte));
            }
            else {
                buf.append((char) ('a' + (halfbyte - 10)));
            }
            halfbyte = data[i] & 0x0F;
        } while(two_halfs++ < 1);
    } 
    return buf.toString();
} 


public static String SHA1(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException  { 
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    byte[] sha1hash = new byte[40];
    md.update(text.getBytes("iso-8859-1"), 0, text.length());
    sha1hash = md.digest();
    return convertToHex(sha1hash);
} 

use the SHA1 method to get your sha1 string.

Update: providing a complete answer

其他推荐答案

here are 2 methods i have found while searching for a sha1 algorithm implementation:

private static String convertToHex(byte[] data) { 
    StringBuffer buf = new StringBuffer();
    int length = data.length;
    for(int i = 0; i < length; ++i) { 
        int halfbyte = (data[i] >>> 4) & 0x0F;
        int two_halfs = 0;
        do { 
            if((0 <= halfbyte) && (halfbyte <= 9)) 
                buf.append((char) ('0' + halfbyte));
            else 
                buf.append((char) ('a' + (halfbyte - 10)));
            halfbyte = data[i] & 0x0F;
        }
        while(++two_halfs < 1);
    } 
    return buf.toString();
}

public static String SHA1(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException  { 
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    byte[] sha1hash = new byte[40];
    md.update(text.getBytes("iso-8859-1"), 0, text.length());
    sha1hash = md.digest();
    return convertToHex(sha1hash);
} 

use the SHA1 method to get your sha1 string. I have not confirmed that this is indeed a sha1, but it works for my apps.

其他推荐答案

I've answered this before (How to SHA1 hash a string in Android?) but it fits here, as well:

Android comes with Apache's Commons Codec so you can simply use the following line to create a SHA-1 hexed String:

String myHexHash = DigestUtils.shaHex(myFancyInput);

That is the old deprecated method you get with Android 4 by default. The new versions of DigestUtils bring all flavors of shaHex() methods like sha256Hex() and also overload the methods with different argument types.

Of course, there is more functionality in DigestUtils and the rest of Commons Codec. Just have a look.

http://commons.apache.org/proper/commons-codec//javadocs/api-release/org/apache/commons/codec/digest/DigestUtils.html

EDIT:

If you get a ClassNotFoundError you will have to explicitly add commons-codec as dependency (even though it should come with Android as transitive dependency), in Maven e.g.:

    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.7</version>
    </dependency>

And also, you will have to change the call to:

String myHexHash = new String(Hex.encodeHex(DigestUtils.sha512(myFancyInput)));

(My humble guess is that this is probably due to a ClassLoader issue (class name collision) in the Android VM - which would actually prove that the commons-codec classes are already present...)

See also: https://stackoverflow.com/a/9284092/621690