Answer the question
In order to leave comments, you need to log in
help translate from PHP to C#
Good afternoon, please help with the code in C #
there is such a function in PHP
public function getCode($secret,$time = null) {
if (!$time) {
$time = floor(time() / 30);
}
$base32 = new FixedBitNotation(5, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', TRUE, TRUE);
$secret = $base32->decode($secret);
$time = pack("N", $time);
$time = str_pad($time,8, chr(0), STR_PAD_LEFT);
$hash = hash_hmac('sha1',$time,$secret,true);
$offset = ord(substr($hash,-1));
$offset = $offset & 0xF;
$truncatedHash = self::hashToInt($hash, $offset) & 0x7FFFFFFF;
$pinValue = str_pad($truncatedHash % self::$PIN_MODULO,6,"0",STR_PAD_LEFT);;
return $pinValue;
}
protected function hashToInt($bytes, $start) {
$input = substr($bytes, $start, strlen($bytes) - $start);
$val2 = unpack("N",substr($input,0,4));
return $val2[1];
}
public static readonly DateTime UNIX_EPOCH = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static string GetPassword(string secret )
{
long counter = (long)((DateTime.UtcNow - UNIX_EPOCH).TotalSeconds ) / 30;
return GeneratePassword(secret, counter);
}
public static string GeneratePassword(string secret, long iterationNumber)
{
int digits = 6;
byte[] counter = BitConverter.GetBytes(iterationNumber);
if (BitConverter.IsLittleEndian)
Array.Reverse(counter);
byte[] key = Encoding.ASCII.GetBytes(secret);
HMACSHA1 hmac = new HMACSHA1(key, true);
byte[] hash = hmac.ComputeHash(counter);
int offset = hash[hash.Length - 1] & 0xf;
int binary =
((hash[offset] & 0x7f) << 24)
| ((hash[offset + 1] & 0xff) << 16)
| ((hash[offset + 2] & 0xff) << 8)
| (hash[offset + 3] & 0xff);
int password = binary % (int)Math.Pow(10, digits); // 6 digits
return password.ToString(new string('0', digits));
}
Answer the question
In order to leave comments, you need to log in
And what's stopping you from turning on the debugger and walking through the code, there are 20 lines of it here.
Questions like this always surprise me.
Do you use the same encoding in C# and PHP? In other words, you should, for example, use UTF-8 both there and there.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question