简体   繁体   English

无法创建正确的 SHA256 哈希

[英]Unable to create the correct SHA256 hash

I am working on a project where i need to create a SHA256 hash as per the steps shown below and compare it with existing hash for verification.我正在做一个项目,我需要按照下面显示的步骤创建一个 SHA256 哈希,并将其与现有的哈希进行比较以进行验证。

Below mentioned are the steps mentioned for creating hash:下面提到的是创建哈希的步骤:

Hashing logic for Mobile Number : 
Sha256(Sha256(Mobile+SharePhrase))*number of times last digit of Aadhaar number
(Ref ID field contains last 4 digits).

Example : 
Mobile: 1234567890
Aadhaar Number:XXXX XXXX 3632
Passcode : Lock@487
Hash: Sha256(Sha256(1234567890Lock@487))*2 

I am doing it this way我这样做

byte[] digCloneOutput = new byte[32];
private void getPhoneHash(String numberToHash) {

    String phn = (getPhn+""+ed_shareCode.getText().toString().trim());

    Log.e("phn", phn);

    MessageDigest md = null;

    byte[] digest;
    byte[] digClone = new byte[0];

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
        digest = phn.getBytes(Charset.forName("UTF-8"));
        digClone = digest.clone();
    }

    try
    {
        md = MessageDigest.getInstance("SHA-256");
    }
    catch (NoSuchAlgorithmException e)
    {
        throw new IllegalStateException("SHA-256 hash should be available", e);
    }

    for(int i = 0; i< 2; i++){
        md.update(digClone);
        digClone = md.digest();
        Log.e("Intermediate hash",""+ digClone);
    }


    for (int i = 0; i < digClone.length; i++)
    {

        digCloneOutput[i] = (byte) (digClone[i]*lastDigAdhr);
        //  md.update(digClone);
        //  printDigest("Intermediate hash", digClone);
    }

    printDigest( digCloneOutput);
}


public  void printDigest(byte[] digest)
{
    StringBuffer hexString = new StringBuffer();
    for (int i = 0; i < digest.length; i++) {
    String hex = Integer.toHexString(0xff & digest[i]);
    if(hex.length() == 1) hexString.append('0');
    hexString.append(hex);
}

    Log.e("Final HashString to compare", String.valueOf(hexString));

}

Final hash with which i have to compare looks like this我必须与之比较的最终哈希看起来像这样

6d0af38001b278389875d2119a187ac5d4df16f5dd75fa5272499059c1149803 6d0af38001b278389875d2119a187ac5d4df16f5dd75fa5272499059c1149803

but the hash created by my logic never matches the original hash.但是我的逻辑创建的散列永远不会匹配原始散列。

I am struggling from last 2 days on this.从过去 2 天开始,我一直在为此苦苦挣扎。 Any help will be appreciated.任何帮助将不胜感激。 Thanks in advance提前致谢

Sorry for the late answer.抱歉回复晚了。 I assume you have already resolved your issue.我假设你已经解决了你的问题。 Hope this answer helps anyone still looking for the answer.希望这个答案可以帮助任何仍在寻找答案的人。

Aadhar website have very vague documentation which makes it harder to develop. Aadhar 网站的文档非常模糊,这使得开发变得更加困难。 Most of us will get confused by looking at the statement: Sha256(Sha256(Mobile+SharePhrase))*number of times last digit of Aadhaar number (at-least I got confused).我们中的大多数人会在查看以下语句时感到困惑: Sha256(Sha256(Mobile+SharePhrase))*number of times last digit of Aadhaar number (至少我感到困惑)。 So I had to use trial & error method to verify the hash and it comes out to be simple logic.所以我不得不使用试错法来验证哈希,结果是简单的逻辑。 All you need to do is concatenate the mobile with Zip password & hash the resulted string number of times last digit of aadhaar no.您需要做的就是将手机与 Zip 密码连接起来并散列结果字符串的次数 aadhaar no 的最后一位。 You can use the below code for the same:您可以使用以下代码:

private boolean isHashMatched(String mobileNo, String zipPassword, String 
    hashedMobile ,int aadharLastDigit){
    String concatedString  = mobileNo + zipPassword;
    aadharLastDigit = aadharLastDigit == 0 ? 1 : aadharLastDigit; //if last 
    //digit is "0", hash only one time.
    try {
        for(int i = 0; i < aadharLastDigit; i++){
            concatedString = DigestUtils.sha256Hex(concatedString);
        }
        return hashedMobile.equals(concatedString);
    }catch (Exception e){
        e.printStackTrace();
        return false;
    }
}

Use Apache Commons-Codec for hashing purposes( implementation 'commons-codec:commons-codec:1.14' ).使用 Apache Commons-Codec 进行哈希处理( implementation 'commons-codec:commons-codec:1.14' )。

For SHA-256 I use this simple code hope it will help you对于 SHA-256 我使用这个简单的代码希望它会帮助你

fun encryptToSha256(data: String): String {
    val digest = MessageDigest.getInstance("SHA-256")
    digest.update(data.toByteArray())
    return bytesToHexString(digest.digest())
}

private fun bytesToHexString(bytes: ByteArray): String {
    val sb = StringBuffer()
    for (i in bytes.indices) {
        val hex = Integer.toHexString(0xFF and bytes[i].toInt())
        if (hex.length == 1) {
            sb.append('0')
        }
        sb.append(hex)
    }
    return sb.toString()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM