-
Notifications
You must be signed in to change notification settings - Fork 495
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expand
DataProtection
sample to illustrate buffer byte access (#1626)
- Loading branch information
Showing
3 changed files
with
16 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ bin | |
*.lock | ||
*.user | ||
*.filters | ||
*.bin |
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 |
---|---|---|
|
@@ -10,4 +10,5 @@ features = [ | |
"Foundation", | ||
"Storage_Streams", | ||
"Security_Cryptography_DataProtection", | ||
"Win32_System_WinRT", | ||
] |
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 |
---|---|---|
@@ -1,16 +1,24 @@ | ||
use windows::{core::*, Security::Cryptography::DataProtection::*, Security::Cryptography::*}; | ||
use windows::{core::*, Security::Cryptography::DataProtection::*, Security::Cryptography::*, Storage::Streams::*, Win32::System::WinRT::*}; | ||
|
||
fn main() -> Result<()> { | ||
fn main() -> std::io::Result<()> { | ||
let provider = DataProtectionProvider::CreateOverloadExplicit("LOCAL=user")?; | ||
let unprotected = CryptographicBuffer::ConvertStringToBinary("Hello world", BinaryStringEncoding::Utf8)?; | ||
|
||
let buffer = CryptographicBuffer::ConvertStringToBinary("Hello world", BinaryStringEncoding::Utf8)?; | ||
|
||
let protected = provider.ProtectAsync(buffer)?.get()?; | ||
let protected = provider.ProtectAsync(unprotected)?.get()?; | ||
let protected_bytes = unsafe { as_mut_bytes(&protected)? }; | ||
std::fs::write("secret.bin", protected_bytes)?; | ||
|
||
let protected_bytes = std::fs::read("secret.bin")?; | ||
let protected = CryptographicBuffer::CreateFromByteArray(&protected_bytes)?; | ||
let unprotected = provider.UnprotectAsync(protected)?.get()?; | ||
|
||
let message = CryptographicBuffer::ConvertBinaryToString(BinaryStringEncoding::Utf8, unprotected)?; | ||
|
||
println!("{}", message); | ||
Ok(()) | ||
} | ||
|
||
unsafe fn as_mut_bytes(buffer: &IBuffer) -> Result<&mut [u8]> { | ||
let interop = buffer.cast::<IBufferByteAccess>()?; | ||
let data = interop.Buffer()?; | ||
Ok(std::slice::from_raw_parts_mut(data, buffer.Length()? as _)) | ||
} |