Skip to content

Commit

Permalink
add steam support for otp generation
Browse files Browse the repository at this point in the history
  • Loading branch information
kspearrin committed Nov 29, 2018
1 parent 7ff628e commit 8175af4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/App/Models/OtpAuth.cs
Expand Up @@ -44,6 +44,12 @@ public OtpAuth(string key)
}
}
}
else if (key?.ToLowerInvariant().StartsWith("steam://") ?? false)
{
Steam = true;
Digits = 5;
Secret = key.Substring(8);
}
else
{
Secret = key;
Expand All @@ -54,5 +60,6 @@ public OtpAuth(string key)
public int Digits { get; set; } = 6;
public MacAlgorithm Algorithm { get; set; } = MacAlgorithm.HmacSha1;
public string Secret { get; set; }
public bool Steam { get; set; }
}
}
20 changes: 18 additions & 2 deletions src/App/Utilities/Crypto.cs
Expand Up @@ -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);
Expand Down Expand Up @@ -203,9 +205,23 @@ public static string Totp(string key)
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
Expand Down

0 comments on commit 8175af4

Please sign in to comment.