private string EncryptWindows(SecureToken data) { if (data == null) { return(null); } #if PROTECTEDDATA return(Convert.ToBase64String(ProtectedData.Protect(data.UseString((ref string s) => Encoding.UTF8.GetBytes(s)) , ConnectionPreferences.Salt, DataProtectionScope.CurrentUser))); #else return(null); #endif }
private bool TryDecryptWindows(string encrypted, out SecureToken decrypted) { decrypted = string.Empty; #if PROTECTEDDATA try { var data = Convert.FromBase64String(encrypted); var decryptedData = ProtectedData.Unprotect(data, Salt, DataProtectionScope.CurrentUser); var chars = Encoding.UTF8.GetChars(decryptedData); decrypted = new SecureToken(ref chars); return(true); } catch (System.Security.Cryptography.CryptographicException) { return(false); } catch (FormatException) { return(false); } #else return(false); #endif }
/// <summary> /// Instantiate an <see cref="ExplicitCredentials"/> instance /// </summary> /// <param name="database">Name of the database</param> /// <param name="username">User name</param> /// <param name="password">Password</param> public ExplicitCredentials(string database, string username, SecureToken password) { Database = database; Username = username; Password = password; }
/// <summary> /// Indicates if a <see cref="SecureToken"/> is null or empty /// </summary> /// <param name="token">The token to check.</param> /// <returns> /// <c>true</c> if <paramref name="token"/> is null or empty (with a <see cref="SecureToken.Length"/> < 0; otherwise, <c>false</c>. /// </returns> public static bool IsNullOrEmpty(this SecureToken token) { return(token == null || token.Length < 1); }
/// <summary> /// Calculate the MD5 checksum of the securely stored value /// </summary> public string CalcMd5(SecureToken value) { return(value.UseBytes((ref byte[] b) => MD5.ComputeHash(b)).ToLowerInvariant()); }