C# 什么变量在特定的固定时间间隔内保持为真?

C# 什么变量在特定的固定时间间隔内保持为真?,c#,time,hash,C#,Time,Hash,我有一个很酷的方法来生成一个6位数的哈希来进行电话验证 代码从不存储,而是通过操纵特定手机加上一些其他变量(包括发出时间)来计算的,这些变量会导致在一小时结束时发出的代码在用户有机会使用之前过期 如何获取在某个时间间隔内保持不变的时间变量以包含它,从而使代码在该时间间隔后自动过期 public string getTimeVariable(long minutes) { var now=DateTime.Now; //Some imprementation I cant think of

我有一个很酷的方法来生成一个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
}

考虑一下这段代码,它只需打开查看上一段时间内哪些代码是有效的就可以了。现在代码每秒钟都在更改。您可以更改期间大小和过去仍被视为有效的期间数

顺便问一下,你最酷的方法是什么

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();
 }
}
使用系统;
使用系统文本;
使用系统线程;
公开课考试{
私有静态长秘密=0xdeadbeef;
专用静态整数位数=6;
专用静态整数周期大小(以秒为单位)=1;
公共字符串电话号码;
公共整数有效期;
专用字符串引用(长增量){
//获取当前分钟(UTC)
long now=DateTime.now.ToUniversalTime().ToFileTimeUtc();
现在/=(周期大小,单位为秒*10000000);
//电话号码因素
var inputBytes=Encoding.ASCII.GetBytes(电话号码);
长mux=0;
foreach(以inputBytes为单位的字节元素){
mux^=元素;
mux=1;//带数字
如果(0!=(now&0x1)){//生成代码
现在>>=1;//阅读LFSR了解更多信息
现在^=secret;//不太确定
}
now=Math.Abs(now);
现在%=mod;//将输出保持在范围内
立即返回.ToString().PadLeft(数字“0”);
}
公共字符串getTimeVariable(){
返回引用(0);
}
公共布尔校验变量(字符串变量){
for(int i=0;i
你需要一些cookie,这些cookie会在一段时间后过期吗?你的问题很不清楚。你不能在任何地方存储代码,你只需将其编译成一个程序集。没有cookie,只是一个变量可能你应该展示一些代码来说明你想要实现什么。好吧,让我来编辑这个问题。你的意思是
var now=DateTime.UtcNow();now=now.Date.AddHours(now.Hour)
?谢谢,这个很酷的方法只是对数字进行散列,并以一种减少冲突的方式转换为一个6位数字。我一到电脑就会尝试这个方法。。。