Private/SecureString/ConvertFrom-SecretSharingPassphraseSecureString.ps1
|
function ConvertFrom-SecretSharingPassphraseSecureString { <# .SYNOPSIS Converts a passphrase SecureString to its UTF-8 byte representation. .DESCRIPTION Unlike ConvertTo-/ConvertFrom-SecretSharingSecureString (which round-trip arbitrary bytes one-byte-per-character for the secret itself), a passphrase is real user-typed text, so it is decoded as UTF-16 characters and re-encoded as UTF-8 bytes, per SLIP-0039's passphrase convention. #> [CmdletBinding()] [OutputType([byte[]], [System.Object[]])] param( [Parameter(Mandatory)] [System.Security.SecureString]$SecureString ) $pointer = [System.Runtime.InteropServices.Marshal]::SecureStringToGlobalAllocUnicode($SecureString) try { $plainText = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($pointer, $SecureString.Length) return , [System.Text.Encoding]::UTF8.GetBytes($plainText) } finally { [System.Runtime.InteropServices.Marshal]::ZeroFreeGlobalAllocUnicode($pointer) } } |