簡體   English   中英

Java中使用遞歸方法的密碼哈希鏈

[英]Cryptographic hash chain using recursive method in Java

我正在嘗試實現一種簡單的遞歸方法,該方法可生成n次指定的哈希值的哈希值,即哈希鏈。 到目前為止,這就是我所擁有的。

import java.io.UnsupportedEncodingException;

public class RecursiveHash {
     public static String generateHashChain(int hash_time, String password)  throws UnsupportedEncodingException{
       hash_time--;
       if (hash_time == 1)  
           return Hash.generateHash(password);  
       return generateHashChain(hash_time,password);
     }

     public static void main(String[] args) {
        try {
            System.out.println(generateHashChain(2,"hello"));
            System.out.println("Test");
            System.out.println(Hash.generateHash((Hash.generateHash("hello"))));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
 }

Hash.generateHash是用於生成哈希的實用程序方法。 hash_time是我要hash_time的次數。 但是,當我像上面一樣測試(哈希兩次)時,我得到了

2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
Test
d7914fe546b684688bb95f4f888a92dfc680603a75f23eb823658031fff766d9

我希望答案是一樣的。 關於做錯什么的任何想法?

System.out.println(generateHashChain(2,"hello"));

這將從2開始該過程。

hash_time--;

現在是1

   if (hash_time == 1)  
       return Hash.generateHash(password);  

1所以只做一次哈希。

要解決您的問題:

 public static String generateHashChain(int hash_time, String password)  throws UnsupportedEncodingException{
   if (hash_time == 1)  
       return Hash.generateHash(password);  
   return Hash.generateHash(generateHashChain(hash_time - 1,password));
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM