1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-11-27 12:26:31 +01:00

add steam support for otp generation

This commit is contained in:
Kyle Spearrin 2018-11-29 17:10:51 -05:00
parent 7ff628ea51
commit 8175af4e84
2 changed files with 25 additions and 2 deletions

View File

@ -44,6 +44,12 @@ namespace Bit.App.Models
}
}
}
else if (key?.ToLowerInvariant().StartsWith("steam://") ?? false)
{
Steam = true;
Digits = 5;
Secret = key.Substring(8);
}
else
{
Secret = key;
@ -54,5 +60,6 @@ namespace Bit.App.Models
public int Digits { get; set; } = 6;
public MacAlgorithm Algorithm { get; set; } = MacAlgorithm.HmacSha1;
public string Secret { get; set; }
public bool Steam { get; set; }
}
}

View File

@ -9,6 +9,8 @@ namespace Bit.App.Utilities
{
public static class Crypto
{
private static string SteamChars = "23456789BCDFGHJKMNPQRTVWXY";
public static CipherString AesCbcEncrypt(byte[] plainBytes, SymmetricCryptoKey key)
{
var parts = AesCbcEncryptToParts(plainBytes, key);
@ -203,9 +205,23 @@ namespace Bit.App.Utilities
var offset = (hash[hash.Length - 1] & 0xf);
var binary = ((hash[offset] & 0x7f) << 24) | ((hash[offset + 1] & 0xff) << 16) |
((hash[offset + 2] & 0xff) << 8) | (hash[offset + 3] & 0xff);
var otp = binary % (int)Math.Pow(10, otpParams.Digits);
return otp.ToString().PadLeft(otpParams.Digits, '0');
string otp = string.Empty;
if(otpParams.Steam)
{
var fullCode = binary & 0x7fffffff;
for(var i = 0; i < otpParams.Digits; i++)
{
otp += SteamChars[fullCode % SteamChars.Length];
fullCode = (int)Math.Truncate(fullCode / (double)SteamChars.Length);
}
}
else
{
var rawOtp = binary % (int)Math.Pow(10, otpParams.Digits);
otp = rawOtp.ToString().PadLeft(otpParams.Digits, '0');
}
return otp;
}
// ref: https://tools.ietf.org/html/rfc5869