RobThree / 2FACLIClient

LastPass 2FA CLI Client
https://lastpass.com
MIT License
6 stars 1 forks source link

decode not working correctly with lowercase secrets #2

Closed urrpurr closed 1 year ago

urrpurr commented 1 year ago

Microsoft accounts in lastpass have secret stored in lowercase characters. I was getting an error untill I added .ToUpper()

    internal static partial class Base32
    {
        private const string _base32alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
        private static readonly Dictionary<char, byte> _base32lookup = _base32alphabet.Select((c, i) => new { c, i }).ToDictionary(v => v.c, v => (byte)v.i);

        public static byte[] Decode(string value)
        {
            ArgumentNullException.ThrowIfNull(value);

            // Remove padding AND making the secret uppercase
            value = value.ToUpper().TrimEnd('=');

            // Decode Base32 value (not world's most efficient or beautiful code but it gets the job done.
            var bits = string.Concat(value.Select(c => Convert.ToString(_base32lookup[c], 2).PadLeft(5, '0')));
            return Enumerable.Range(0, bits.Length / 8).Select(i => Convert.ToByte(bits.Substring(i * 8, 8), 2)).ToArray();
        }
    }
RobThree commented 1 year ago

Again... weeeeeeird. In over 100 accounts I couldn't find a single one with lowercase secret, even several Microsoft accounts. Though it won't hurt to do a ToUpperInvariant() for good measure. I'll add that in.