Beispiel #1
0
 public static bool validatePlainSecret(string secret, OtpSecretEncoding encoding)
 {
     if (encoding == OtpSecretEncoding.Base32)
     {
         if (Regex.IsMatch(secret, @"^(?:[A-Z2-7]{8})*(?:[A-Z2-7]{2}={6}|[A-Z2-7]{4}={4}|[A-Z2-7]{5}={3}|[A-Z2-7]{7}=)?$"))
         {
             return(true);
         }
         else
         {
             throw new InvalidBase32FormatException("Invalid Base32 format!\n\nRequired length: 8 * n (if shorter, fill up with =)\n\nAllowed characters:\nABCDEFGHIJKLMNOPQRSTUVWXYZ234567=\n\nRegex:\n^(?:[A-Z2-7]{8})*(?:[A-Z2-7]{2}={6}|[A-Z2-7]{4}={4}|[A-Z2-7]{5}={3}|[A-Z2-7]{7}=)+$");
         }
     }
     else if (encoding == OtpSecretEncoding.Base64)
     {
         if (Regex.IsMatch(secret, @"^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$"))
         {
             return(true);
         }
         else
         {
             throw new InvalidBase64FormatException("Invalid Base32 format!\n\nRequired length: 4 * n (if shorter, fill up with =)\n\nAllowed characters:\nabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789=\n\nRegex:\n^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$");
         }
     }
     else if (encoding == OtpSecretEncoding.Hex)
     {
         if (Regex.IsMatch(secret, @"^([A-Fa-f0-9]{2})+$"))
         {
             return(true);
         }
         else
         {
             throw new InvalidHexFormatException("Invalid Hex format!\n\nRequired length: 2 * n\n\nAllowed characters:\nabcdefABCDEF0123456789\n\nRegex:\n^([a-fA-F0-9]{2})+$");
         }
     }
     else if (encoding == OtpSecretEncoding.UTF8)
     {
         return(true);
     }
     throw new InvalidOperationException("No Encoding given!");
 }
Beispiel #2
0
        public static string correctPlainSecret(string secret, OtpSecretEncoding encoding)
        {
            secret = secret.Replace("=", "").Replace(" ", "");
            int secretLength = secret.Length;

            if (encoding == OtpSecretEncoding.Base32)
            {
                secret = secret.ToUpper();
                if (secretLength % 8 == 2 || secretLength % 8 == 4 || secretLength % 8 == 5 || secretLength % 8 == 7)
                {
                    secret += new string('=', 8 - secretLength % 8);
                }
            }
            else if (encoding == OtpSecretEncoding.Base64)
            {
                if (secretLength % 4 == 2 || secretLength % 4 == 3)
                {
                    secret += new string('=', 4 - secretLength % 4);
                }
            }

            return(secret);
        }