2017-08-27 51 views
-1

我有一個很酷的方法來生成一個6位數的散列用於電話驗證。什麼變量在特定的固定時間間隔內保持真實?

該代碼永遠不會被存儲,而是通過操縱該特定手機加上一些其他變量(包括導致在小時結束時發佈的代碼在用戶有機會使用它們之前到期的發佈時間)來計算的。

我怎麼能拿到一定的時間間隔內保持不變,包括它使代碼後自動失效時間變量?

public string getTimeVariable(long minutes) 
{ 
    var now=DateTime.Now; 
    //Some imprementation I cant think of.... 
} 

public bool verifyVariable(string variable) 
{ 
    //Some other implementation to return true if specified minutes haven't elapsed since variable was issued 
} 
+0

您需要一些在一段時間後過期的cookie嗎?你的問題很不明確。你不能在任何地方存儲代碼,你只需將它編譯成一個程序集。 – HimBromBeere

+0

沒有cookie只是一個變量 – konzo

+0

也許你應該展示一些代碼來說明你想實現什麼。 – HimBromBeere

回答

1

考慮一下這段代碼,它只是簡單地開放以查看哪些代碼在上一段時間內是有效的。代碼現在每秒更改一次。您可以更改期限大小以及過去仍被視爲有效的期數。

順便說一句 - 你有什麼酷的方法是什麼呢?

using System; 
using System.Text; 
using System.Threading; 

public class Test { 

private static long secret = 0xdeadbeef; 
private static int digits = 6; 
private static int period_size_in_seconds = 1; 

public string phonenumber; 
public int validperiods; 

private string reference(long delta) { 
    // get current minute (UTC) 
    long now = DateTime.Now.ToUniversalTime().ToFileTimeUtc(); 
    now /= (period_size_in_seconds * 10000000); 
    // factor in phone number 
    var inputBytes = Encoding.ASCII.GetBytes(phonenumber); 
    long mux = 0; 
    foreach(byte elem in inputBytes) { 
    mux ^= elem; 
    mux <<= 1; 
    } 
    // limit number of digits 
    long mod = Convert.ToInt64(Math.Pow(10, digits)) - 1; // how many digits? 
    // apply time shift for validations 
    now -= delta; 
    // and play a little bit 
    now *= mux; // factor in phone number 
    now ^= secret; // play a bit 
    now >>= 1; // with the number 
    if (0 != (now & 0x1)) { // to make the code 
    now >>= 1; // read about LFSR to learn more 
    now ^= secret; // less deterministic 
    } 
    now = Math.Abs(now); 
    now %= mod; // keep the output in range 
    return now.ToString().PadLeft(digits, '0'); 
} 

public string getTimeVariable() { 
    return reference(0); 
} 

public bool verifyVariable(string variable) { 
    for (int i = 0; i < validperiods; i++) { 
    if (variable == reference(i)) { 
    return true; 
    } 
    } 
    return false; 
} 

public void test() { 
    phonenumber = "+48602171819"; 
    validperiods = 900; 
    string code = getTimeVariable(); 

    if (verifyVariable(getTimeVariable())) 
    System.Console.Write("OK1"); 

    if (verifyVariable(code)) 
    System.Console.Write(" OK2"); 

    Thread.Sleep(2*1000*period_size_in_seconds); 

    if (verifyVariable(code)) { 
    System.Console.WriteLine(" OK3"); 
    } 

    System.Console.WriteLine(code); 
} 

public static void Main() { 
    (new Test()).test(); 
} 
} 
+0

致謝成才,清涼的方法只是哈希NA的數量和變換是在減少collition的方式6位數,我會盡快嘗試了這一點,因爲我到達我的電腦... – konzo

+0

那麼工作謝謝你的時間.. – konzo

相關問題