-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from richardschneider/secure-string
Use a Secure String
- Loading branch information
Showing
6 changed files
with
140 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
using System.Security; | ||
namespace Ipfs.Engine.Cryptography | ||
{ | ||
/// <summary> | ||
/// Extensions for a <see cref="SecureString"/>. | ||
/// </summary> | ||
public static class SecureStringExtensions | ||
{ | ||
/// <summary> | ||
/// Use the plain bytes of a <see cref="SecureString"/>. | ||
/// </summary> | ||
/// <param name="s">The secure string to access.</param> | ||
/// <param name="action"> | ||
/// A function to call with the plain bytes. | ||
/// </param> | ||
public static void UseSecretBytes(this SecureString s, Action<byte[]> action) | ||
{ | ||
var length = s.Length; | ||
var p = SecureStringMarshal.SecureStringToGlobalAllocAnsi(s); | ||
var plain = new byte[length]; | ||
try | ||
{ | ||
Marshal.Copy(p, plain, 0, length); | ||
action(plain); | ||
} | ||
finally | ||
{ | ||
Marshal.ZeroFreeGlobalAllocAnsi(p); | ||
p = IntPtr.Zero; | ||
Array.Clear(plain, 0, length); | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Security; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Ipfs.Engine.Cryptography | ||
{ | ||
[TestClass] | ||
public class SecureStringExtensionsTest | ||
{ | ||
[TestMethod] | ||
public void UseBytes() | ||
{ | ||
var secret = new SecureString(); | ||
var expected = new char[] { 'a', 'b', 'c' }; | ||
foreach (var c in expected) secret.AppendChar(c); | ||
secret.UseSecretBytes(bytes => | ||
{ | ||
Assert.AreEqual(expected.Length, bytes.Length); | ||
for (var i = 0; i < expected.Length; ++i) | ||
Assert.AreEqual((int)expected[i], (int)bytes[i]); | ||
}); | ||
} | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters