Одноразовый пароль на основе HMAC в C # (RFC 4226 - HOTP)

Я пытаюсь сосредоточиться на создании 6-значного / символьного одноразового пароля с истекающим сроком действия без учета регистра.

Мой источник - http://tools.ietf.org/html/rfc4226#section -5

Сначала определение параметров

C       8-byte counter value, the moving factor.  This counter
       MUST be synchronized between the HOTP generator (client)
       and the HOTP validator (server).

K       shared secret between client and server; each HOTP
       generator has a different and unique secret K.

T       throttling parameter: the server will refuse connections
       from a user after T unsuccessful authentication attempts.

Затем у нас есть алгоритм для генерации HOTP

As the output of the HMAC-SHA-1 calculation is 160 bits, we must
   truncate this value to something that can be easily entered by a
   user.

                   HOTP(K,C) = Truncate(HMAC-SHA-1(K,C))

Затем у нас есть Truncate, определенный как

String = String[0]...String[19]
 Let OffsetBits be the low-order 4 bits of String[19]
 Offset = StToNum(OffsetBits) // 0 <= OffSet <= 15
 Let P = String[OffSet]...String[OffSet+3]
 Return the Last 31 bits of P

И затем предлагается пример для 6-значного HOTP

The following code example describes the extraction of a dynamic
binary code given that hmac_result is a byte array with the HMAC-
SHA-1 result:

    int offset   =  hmac_result[19] & 0xf ;
    int bin_code = (hmac_result[offset]  & 0x7f) << 24
       | (hmac_result[offset+1] & 0xff) << 16
       | (hmac_result[offset+2] & 0xff) <<  8
       | (hmac_result[offset+3] & 0xff) ;

Я затрудняюсь преобразовать это в полезный код C # для генерации одноразовых паролей. У меня уже есть следующий код для создания HMAC с истекающим сроком действия:

byte[] hashBytes = alg.ComputeHash(Encoding.UTF8.GetBytes(input));
byte[] result = new byte[8 + hashBytes.Length];

hashBytes.CopyTo(result, 8);
BitConverter.GetBytes(expireDate.Ticks).CopyTo(result, 0);

Я просто не знаю, как перейти от этого к 6-значному, как предлагается в приведенных выше алгоритмах.

6
задан Josh 29 November 2010 в 20:53
поделиться