Authenticator.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Linq;
  3. using System.Security.Cryptography;
  4. namespace InABox.Core
  5. {
  6. public static class Authenticator
  7. {
  8. public static readonly int CODE_LENGTH = 6;
  9. private static readonly int CODE_MODULO = (int)Math.Pow(10, CODE_LENGTH);
  10. private static byte[] FromHexString(string hex) {
  11. if (hex.Length % 2 == 1)
  12. throw new Exception("The binary key cannot have an odd number of digits");
  13. byte[] arr = new byte[hex.Length >> 1];
  14. for (int i = 0; i < hex.Length >> 1; ++i)
  15. {
  16. arr[i] = (byte)((GetHexVal(hex[i << 1]) << 4) + (GetHexVal(hex[(i << 1) + 1])));
  17. }
  18. return arr;
  19. }
  20. public static int GetHexVal(char hex) {
  21. int val = (int)hex;
  22. //For uppercase A-F letters:
  23. //return val - (val < 58 ? 48 : 55);
  24. //For lowercase a-f letters:
  25. //return val - (val < 58 ? 48 : 87);
  26. //Or the two combined, but a bit slower:
  27. return val - (val < 58 ? 48 : (val < 97 ? 55 : 87));
  28. }
  29. public static string GenerateGoogleAuthenticatorCode(byte[] key)
  30. {
  31. var _time = DateTimeOffset.Now.ToUnixTimeSeconds();
  32. return GenerateGoogleAuthenticatorCode(_time, key);
  33. }
  34. private static string GenerateGoogleAuthenticatorCode(long time, byte[] key)
  35. {
  36. var _window = time / 30;
  37. var _hex = _window.ToString("x");
  38. if (_hex.Length < 16)
  39. {
  40. _hex = _hex.PadLeft(16, '0');
  41. }
  42. var _bytes = FromHexString(_hex);
  43. var _hash = new HMACSHA1(key).ComputeHash(_bytes);
  44. var _offset = _hash.Last() & 0xf;
  45. var _selected = new byte[4];
  46. Buffer.BlockCopy(_hash, _offset, _selected, 0, 4);
  47. if (BitConverter.IsLittleEndian)
  48. {
  49. Array.Reverse(_selected);
  50. }
  51. var _integer = BitConverter.ToInt32(_selected, 0);
  52. var _truncated = _integer & 0x7fffffff;
  53. return (_truncated % CODE_MODULO).ToString().PadLeft(CODE_LENGTH, '0');
  54. }
  55. public static bool CheckAuthenticationCode(byte[] key, string code)
  56. {
  57. var _time = DateTimeOffset.Now.ToUnixTimeSeconds();
  58. for (long _l = _time - 30; _l <= _time; _l += 30)
  59. {
  60. if(GenerateGoogleAuthenticatorCode(_l, key) == code)
  61. {
  62. return true;
  63. }
  64. }
  65. return false;
  66. }
  67. }
  68. }