-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Set `Password` to `SecureString` for Wallet information * Added `UnitTest` for `SecureStringExtensions` * Update tests/Neo.Extensions.Tests/UT_SecureStringExtensions.cs Co-authored-by: Shargon <shargon@gmail.com> * Update src/Neo.Extensions/SecureStringExtensions.cs --------- Co-authored-by: Shargon <shargon@gmail.com>
- Loading branch information
1 parent
0d46c20
commit 5fb2262
Showing
6 changed files
with
106 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,53 @@ | ||
// Copyright (C) 2015-2024 The Neo Project. | ||
// | ||
// SecureStringExtensions.cs file belongs to the neo project and is free | ||
// software distributed under the MIT software license, see the | ||
// accompanying file LICENSE in the main directory of the | ||
// repository or http://www.opensource.org/licenses/mit-license.php | ||
// for more details. | ||
// | ||
// Redistribution and use in source and binary forms with or without | ||
// modifications are permitted. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
using System.Security; | ||
|
||
namespace Neo.Extensions | ||
{ | ||
public static class SecureStringExtensions | ||
{ | ||
public static string? GetClearText(this SecureString secureString) | ||
{ | ||
if (secureString is null) | ||
throw new ArgumentNullException(nameof(secureString)); | ||
|
||
var unmanagedStringPtr = IntPtr.Zero; | ||
|
||
try | ||
{ | ||
unmanagedStringPtr = Marshal.SecureStringToGlobalAllocUnicode(secureString); | ||
return Marshal.PtrToStringUni(unmanagedStringPtr); | ||
} | ||
finally | ||
{ | ||
Marshal.ZeroFreeGlobalAllocUnicode(unmanagedStringPtr); | ||
} | ||
} | ||
|
||
public static SecureString ToSecureString(this string value, bool asReadOnly = true) | ||
{ | ||
unsafe | ||
{ | ||
fixed (char* passwordChars = value) | ||
{ | ||
var securePasswordString = new SecureString(passwordChars, value.Length); | ||
|
||
if (asReadOnly) | ||
securePasswordString.MakeReadOnly(); | ||
return securePasswordString; | ||
} | ||
} | ||
} | ||
} | ||
} |
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,35 @@ | ||
// Copyright (C) 2015-2024 The Neo Project. | ||
// | ||
// UT_SecureStringExtensions.cs file belongs to the neo project and is free | ||
// software distributed under the MIT software license, see the | ||
// accompanying file LICENSE in the main directory of the | ||
// repository or http://www.opensource.org/licenses/mit-license.php | ||
// for more details. | ||
// | ||
// Redistribution and use in source and binary forms with or without | ||
// modifications are permitted. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Neo.Extensions.Tests | ||
{ | ||
[TestClass] | ||
public class UT_SecureStringExtensions | ||
{ | ||
[TestMethod] | ||
public void Test_String_To_SecureString() | ||
{ | ||
var expected = "Hello World"; | ||
var expectedSecureString = expected.ToSecureString(); | ||
|
||
var actual = expectedSecureString.GetClearText(); | ||
|
||
Assert.IsTrue(expectedSecureString.IsReadOnly()); | ||
Assert.AreEqual(expected, actual); | ||
} | ||
} | ||
} |
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