这段代码。。。是什么意思。。。硬是没有看懂。。有什么意义。。计算出来的。。有什么用昵。。特别是后面那些计算。。。硬是没有搞懂。。。。。。。。。。知道的来看一下。

xghabc 2014-06-21 03:18:20
public static int OneTimePassword { set; get; }



byte[] Hmac = new HMACSHA1(***).ComputeHash(***);
int Offset = Hmac.Last() & 0x0F;
OneTimePassword = (
((Hmac[Offset + 0] & 0x7f) << 24) |
((Hmac[Offset + 1] & 0xff) << 16) |
((Hmac[Offset + 2] & 0xff) << 8) |
(Hmac[Offset + 3] & 0xff)
) % 1000000;


没分了。。
...全文
205 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
save4me 2014-06-22
  • 打赏
  • 举报
回复
HMAC是密钥相关的哈希运算消息认证码(Hash-based Message Authentication Code) 你的代码是Google Authenticator 下面是Google Authenticator中一篇文章对这对代码解释的中文翻译

private void CalculateOneTimePassword() {
  // Get the number of seconds since 1/1/1970 and devide them by 30 seconds.
  // Thus one Timestamp unit is 30 seconds.
  //获取从1970年1月1日到现在的秒数,再除以30得到一个单位为30秒的时间戳
  Timestamp = Convert.ToInt64(GetUnixTimestamp() / 30);
 
  // Convert the 64 bit integer Timestamp to a byte array (8 bytes).
  // eg. ba d9 c7 02 00 00 00 00
  // Then reverse them (=> 00 00 00 00 02 c7 d9 ba) and write the result to the byte array "data".
  //把64位整数的时间戳转成8个字节的自己数组
   var data = BitConverter.GetBytes(Timestamp).Reverse().ToArray();
 
  // Generate the Hmac key from your password (byte array) and time (byte array).
  //从你的密码(字节数组)和时间(自己数组)生成哈希运算消息认证码
  Hmac = new HMACSHA1(Secret).ComputeHash(data);
 
  // Bit-operation: Get the last 4 bits of the Hmac. The results are always equal to or between 0 and 15.
  // The offset determines the area of the Hmac that is used to generate the time based password.
  //位运算:得到哈希运算消息认证码的最后4个字节。结果总是等于或者在0到15之间
  Offset = Hmac.Last() & 0x0F;
 
  // The Hmac is 20 bytes long. A block of 4 bytes is used for the OneTimePassword, which changes each 30 seconds.
  // 15 is the highest Offset. Therefore the last used byte is number 18 (first byte is zero based).
  // The 19th (=last) byte is the Offset. More precisely the <a href="http://en.wikipedia.org/wiki/Nibble" title="Wiki Nibble Byte" target="_blank">right nibble</a> of the 19th byte is the Offset value.
  // Bit masks are applied on the selected Hmac block to limit the number. The resulting bits are rotated to the left and added together.
  // Basically we are looking at a manual "bit to integer" conversion.
  // the result is then devided by 1,000,000 and only the remainder is taken. Consequently all results are less than 1,000,000.
  // (The bit mask 0xff is useless. I guess it was used to emphasize the technique for readability purposes. 0x7f does make sense.)
  //哈希运算消息认证码为20字节。OneTimePassword是一个4字节的块,每30秒改变一次。
  //最高位置是15。所以最后一个字节是数字18(第一个自己是以0位基数的)。
  //第19(最后)字节是结束。更精确的说第19个字节是结束值,参考 <a href="http://en.wikipedia.org/wiki/Nibble" title="Wiki Nibble Byte" target="_blank">right nibble</a>
  //位掩码被于选定的哈希运算消息认证码块来限制位数。位计算结果左转并加到一起。
  //基本上我们看到的是一个手工的位到整数的转换
  //结果用1,000,000除,并只保留余数。因此所有的结果都小于1,000,000。
  //(位掩码0xff没有什么作用。我猜用它只为了更直观的强调技术。0x7f是有用的)
  OneTimePassword = (
         ((Hmac[Offset + 0] & 0x7f) << 24) |
         ((Hmac[Offset + 1] & 0xff) << 16) |
         ((Hmac[Offset + 2] & 0xff) << 8) |
         (Hmac[Offset + 3] & 0xff)) % 1000000;
}
save4me 2014-06-22
  • 打赏
  • 举报
回复
搜索引擎就是强啊
引用 7 楼 xghabc 的回复:
靠。。你居然知道了。。
於黾 2014-06-22
  • 打赏
  • 举报
回复
我猜这个应该是游戏里用的吧,比如某个关口的密码,你必须先获得密码书,才能进入关口 否则玩过一次就知道密码了,不获取密码书也能直接进去。 %1000000之后,可以保证密码是纯数字而且不超过6位。
於黾 2014-06-22
  • 打赏
  • 举报
回复

//这不就是自己做了个加密方式,获得密码么,而且标注了是一次性的密码,也就是说明文来源随机,不可重复
 byte[] Hmac = new HMACSHA1(***).ComputeHash(***);//获取个随机的什么东西,然后将它散列了
            int Offset = Hmac.Last() & 0x0F;//根据最后一个元素计算取值位置,只取0-15之间的数字
            OneTimePassword = (//然后把这个元素开始的连续4个字节拿出来当个有符号int型,并计算对1000000取余数
                ((Hmac[Offset + 0] & 0x7f) << 24) |
                ((Hmac[Offset + 1] & 0xff) << 16) |
                ((Hmac[Offset + 2] & 0xff) << 8) |
                (Hmac[Offset + 3] & 0xff)
                    ) % 1000000;
xghabc 2014-06-22
  • 打赏
  • 举报
回复
引用 6 楼 save4me 的回复:
HMAC是密钥相关的哈希运算消息认证码(Hash-based Message Authentication Code) 你的代码是Google Authenticator 下面是Google Authenticator中一篇文章对这对代码解释的中文翻译

private void CalculateOneTimePassword() {
  // Get the number of seconds since 1/1/1970 and devide them by 30 seconds.
  // Thus one Timestamp unit is 30 seconds.
  //获取从1970年1月1日到现在的秒数,再除以30得到一个单位为30秒的时间戳
  Timestamp = Convert.ToInt64(GetUnixTimestamp() / 30);
 
  // Convert the 64 bit integer Timestamp to a byte array (8 bytes).
  // eg. ba d9 c7 02 00 00 00 00
  // Then reverse them (=> 00 00 00 00 02 c7 d9 ba) and write the result to the byte array "data".
  //把64位整数的时间戳转成8个字节的自己数组
   var data = BitConverter.GetBytes(Timestamp).Reverse().ToArray();
 
  // Generate the Hmac key from your password (byte array) and time (byte array).
  //从你的密码(字节数组)和时间(自己数组)生成哈希运算消息认证码
  Hmac = new HMACSHA1(Secret).ComputeHash(data);
 
  // Bit-operation: Get the last 4 bits of the Hmac. The results are always equal to or between 0 and 15.
  // The offset determines the area of the Hmac that is used to generate the time based password.
  //位运算:得到哈希运算消息认证码的最后4个字节。结果总是等于或者在0到15之间
  Offset = Hmac.Last() & 0x0F;
 
  // The Hmac is 20 bytes long. A block of 4 bytes is used for the OneTimePassword, which changes each 30 seconds.
  // 15 is the highest Offset. Therefore the last used byte is number 18 (first byte is zero based).
  // The 19th (=last) byte is the Offset. More precisely the <a href="http://en.wikipedia.org/wiki/Nibble" title="Wiki Nibble Byte" target="_blank">right nibble</a> of the 19th byte is the Offset value.
  // Bit masks are applied on the selected Hmac block to limit the number. The resulting bits are rotated to the left and added together.
  // Basically we are looking at a manual "bit to integer" conversion.
  // the result is then devided by 1,000,000 and only the remainder is taken. Consequently all results are less than 1,000,000.
  // (The bit mask 0xff is useless. I guess it was used to emphasize the technique for readability purposes. 0x7f does make sense.)
  //哈希运算消息认证码为20字节。OneTimePassword是一个4字节的块,每30秒改变一次。
  //最高位置是15。所以最后一个字节是数字18(第一个自己是以0位基数的)。
  //第19(最后)字节是结束。更精确的说第19个字节是结束值,参考 <a href="http://en.wikipedia.org/wiki/Nibble" title="Wiki Nibble Byte" target="_blank">right nibble</a>
  //位掩码被于选定的哈希运算消息认证码块来限制位数。位计算结果左转并加到一起。
  //基本上我们看到的是一个手工的位到整数的转换
  //结果用1,000,000除,并只保留余数。因此所有的结果都小于1,000,000。
  //(位掩码0xff没有什么作用。我猜用它只为了更直观的强调技术。0x7f是有用的)
  OneTimePassword = (
         ((Hmac[Offset + 0] & 0x7f) << 24) |
         ((Hmac[Offset + 1] & 0xff) << 16) |
         ((Hmac[Offset + 2] & 0xff) << 8) |
         (Hmac[Offset + 3] & 0xff)) % 1000000;
}
靠。。你居然知道了。。
tcmakebest 2014-06-21
  • 打赏
  • 举报
回复
有些算法不需要搞懂,只需要一个与众不同的结果就行. 就像是MD5算法它的结果毫无意义,就是与众不同.
自然框架 2014-06-21
  • 打赏
  • 举报
回复
没看出来。断点试一下
xghabc 2014-06-21
  • 打赏
  • 举报
回复
自己坐个沙发。。

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧