MD5 hashing in Android

前端 未结 16 1947
醉梦人生
醉梦人生 2020-11-29 18:06

I have a simple android client which needs to \'talk\' to a simple C# HTTP listener. I want to provide a basic level of authentication by passing username/password in POST r

相关标签:
16条回答
  • 2020-11-29 19:01

    If using Apache Commons Codec is an option, then this would be a shorter implementation:

    String md5Hex = new String(Hex.encodeHex(DigestUtils.md5(data)));
    

    Or SHA:

    String shaHex= new String(Hex.encodeHex(DigestUtils.sha("textToHash")));
    

    Source for above.

    Please follow the link and upvote his solution to award the correct person.


    Maven repo link: https://mvnrepository.com/artifact/commons-codec/commons-codec

    Current Maven dependency (as of 6 July 2016):

    <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.10</version>
    </dependency>
    
    0 讨论(0)
  • 2020-11-29 19:03

    Here is an implementation you can use (updated to use more up to date Java conventions - for:each loop, StringBuilder instead of StringBuffer):

    public static String md5(final String s) {
        final String MD5 = "MD5";
        try {
            // Create MD5 Hash
            MessageDigest digest = java.security.MessageDigest
                    .getInstance(MD5);
            digest.update(s.getBytes());
            byte messageDigest[] = digest.digest();
    
            // Create Hex String
            StringBuilder hexString = new StringBuilder();
            for (byte aMessageDigest : messageDigest) {
                String h = Integer.toHexString(0xFF & aMessageDigest);
                while (h.length() < 2)
                    h = "0" + h;
                hexString.append(h);
            }
            return hexString.toString();
    
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
    

    Although it is not recommended for systems that involve even the basic level of security (MD5 is considered broken and can be easily exploited), it is sometimes enough for basic tasks.

    0 讨论(0)
  • 2020-11-29 19:03

    The accepted answer didn't work for me in Android 2.2. I don't know why, but it was "eating" some of my zeros (0) . Apache commons also didn't work on Android 2.2, because it uses methods that are supported only starting from Android 2.3.x. Also, if you want to just MD5 a string, Apache commons is too complex for that. Why one should keep a whole library to use just a small function from it...

    Finally I found the following code snippet here which worked perfectly for me. I hope it will be useful for someone...

    public String MD5(String md5) {
       try {
            java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
            byte[] array = md.digest(md5.getBytes("UTF-8"));
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < array.length; ++i) {
              sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1,3));
           }
            return sb.toString();
        } catch (java.security.NoSuchAlgorithmException e) {
        } catch(UnsupportedEncodingException ex){
        }
        return null;
    }
    
    0 讨论(0)
  • 2020-11-29 19:03

    I have made a simple Library in Kotlin.

    Add at Root build.gradle

    allprojects {
            repositories {
                ...
                maven { url 'https://jitpack.io' }
            }
        }
    

    at App build.gradle

    implementation 'com.github.1AboveAll:Hasher:-SNAPSHOT'
    

    Usage

    In Kotlin

    val ob = Hasher()
    

    Then Use hash() method

    ob.hash("String_You_Want_To_Encode",Hasher.MD5)
    
    ob.hash("String_You_Want_To_Encode",Hasher.SHA_1)
    

    It will return MD5 and SHA-1 Respectively.

    More about the Library

    https://github.com/ihimanshurawat/Hasher

    0 讨论(0)
提交回复
热议问题