2017-08-27 50 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

相关问题