public static string Base32Decode(string base32EncodedData) { var base32EncodedBytes = Base32Encoding.ToBytes(base32EncodedData); return(Encoding.ASCII.GetString(base32EncodedBytes)); }
public static byte[] Base32DecodeBytes(string base32EncodedData) { return(Base32Encoding.ToBytes(base32EncodedData)); }
public async Task TotpTest() { var totp = new Totp(Base32Encoding.ToBytes(totpSecret)); var totpCode = totp.ComputeTotp(); }
public static bool IsValid(string str) { if (string.IsNullOrWhiteSpace(str)) { return(false); } var uri = new Uri(str); if (!System.Uri.IsWellFormedUriString(str, UriKind.Absolute)) { return(false); } if (uri.Scheme != "otpauth") { return(false); } if (uri.Host != "totp") { return(false); } if (string.IsNullOrWhiteSpace(uri.AbsolutePath.TrimStart('/'))) { return(false); } var label = uri.AbsolutePath.TrimStart('/'); if (string.IsNullOrWhiteSpace(label)) { return(false); } var parsed = HttpUtility.ParseQueryString(uri.Query); var issuer = parsed["issuer"]; if (string.IsNullOrWhiteSpace(issuer)) { return(false); } var secret = parsed["secret"]; if (string.IsNullOrWhiteSpace(secret)) { return(false); } try { var key = Base32Encoding.ToBytes(secret); var totp = new Totp(key); var code = totp.ComputeTotp(); if (string.IsNullOrWhiteSpace(code)) { return(false); } } catch (Exception) { return(false); } return(true); }
private long TimeFromFilename(FileSystemPath path) { var bytes = Base32Encoding.ToBytes(path.EntityName.Split('-')[0]); return(BitConverter.ToInt32(bytes, 0)); }
public static string GenerateCode(string secret, int Algorithm, int Digits, int Period) { var totp = new Totp(Base32Encoding.ToBytes(secret), mode: (OtpHashMode)Algorithm, totpSize: Digits, step: Period); return(totp.ComputeTotp()); }
public Totp(string secret, int period, OtpHashMode algorithm, int digits) { var secretBytes = Base32Encoding.ToBytes(secret); _totp = new OtpNet.Totp(secretBytes, period, algorithm, digits); }
/// <summary> /// Asks the user to choose a password file, decrypts it, /// generates an OTP code from the secret in the totp field, and copies the resulting value to the clipboard. /// </summary> public void GenerateTotpCode(bool copyToClipboard, bool typeTotpCode) { var selectedFile = RequestPasswordFile(); // If the user cancels their selection, the password decryption should be cancelled too. if (selectedFile == null) { return; } KeyedPasswordFile passFile; try { passFile = passwordManager.DecryptPassword(selectedFile, true); } catch (Exception e) when(e is GpgError || e is GpgException || e is ConfigurationException) { notificationService.ShowErrorWindow("TOTP decryption failed: " + e.Message); return; } catch (Exception e) { notificationService.ShowErrorWindow($"TOTP decryption failed: An error occurred: {e.GetType().Name}: {e.Message}"); return; } String secretKey = null; foreach (var k in passFile.Keys) { // totp: Xxxx4 if (k.Key == "totp") { secretKey = k.Value; } // otpauth: //totp/account?secret=FxxxX&digits=6 if (k.Key == "otpauth") { var regex = new Regex("secret=([a-zA-Z0-9]+)&", RegexOptions.Compiled | RegexOptions.IgnoreCase); var matches = regex.Match(k.Value); if (matches.Success) { secretKey = matches.Groups[1].Value; } } } var foo = String.Join(",", passFile.Keys.Select(f => f.Key)); if (secretKey == null) { notificationService.ShowErrorWindow($"TOTP decryption failed: Failed to find an OTP secret. Keys = ${foo}"); return; } var secretKeyBytes = Base32Encoding.ToBytes(secretKey); var totp = new Totp(secretKeyBytes); var totpCode = totp.ComputeTotp(); if (copyToClipboard) { ClipboardHelper.Place(totpCode, TimeSpan.FromSeconds(ConfigManager.Config.Interface.ClipboardTimeout)); if (ConfigManager.Config.Notifications.Types.TotpCopied) { notificationService.Raise($"The totp code has been copied to your clipboard.\nIt will be cleared in {ConfigManager.Config.Interface.ClipboardTimeout:0.##} seconds.", Severity.Info); } } if (typeTotpCode) { KeyboardEmulator.EnterText(totpCode, ConfigManager.Config.Output.DeadKeys); } }
public SteamOtp(string secret) { var secretBytes = Base32Encoding.ToBytes(secret); _totp = new SteamTotp(secretBytes); }