0

I am trying to convert a shell script to PowerShell script, I am facing issue with below shell commands

label=`echo -n "signing key" | hexdump -ve '/1 "%02x"'`
sign_nist="00000001${label}0000000100"

/testing$ echo $sign_nist
000000017369676e696e67206b65790000000100
/testing$ echo -n $sign_nist | sed -e 's/../\\x&/g'
\x00\x00\x00\x01\x73\x69\x67\x6e\x69\x6e\x67\x20\x6b\x65\x79\x00\x00\x00\x01\x00
/testing$ echo -ne "$(echo -n $sign_nist | sed -e 's/../\\x&/g')"
signing key

when I try to do the same thing in PowerShell using the below commands my output is different

PS C:\testing> $sign_nist
000000017369676E696E67206B65790000000100

PS C:\testing> 
$prep = $sign_nist -split '(..)' -ne ''
for($i = 0; $i -lt $prep.length; $i++)
{ 
    $prep[$i] = [System.Convert]::ToUInt32($prep[$i],16) 
}
$sign_key_input=$prep -join '';
$sign_key_input
00011151051031101051101033210710112100010

could someone please help me how to get signing key to $sign_key_input with PowerShell? Thanks in advance for the help.

I think appending \x for each byte works for linux to treat it as hex value but how to indicate the same in powershell?

  • 1
    I'm not sure what you're trying to achieve but `'000000017369676E696E67206B65790000000100' -replace '(?<=^|\G.{2})(?!$)', '\x'` would get you the same output you get on Linux – Santiago Squarzon Nov 25 '22 at 20:22
  • You are using ToUint32 which should be taking 8 digits at a time and converting to a number. You also do not want to change order of the characters. So there are two things to consider. First you are only getting one character at a time using $i++ and should be using $i += 2. Then you should be using [System.Convert]::ToByte($prep[$i], 2). You input is a string so you are taking characters not digits. – jdweng Nov 25 '22 at 21:25
  • 1
    It looks like you're trying to convert string `'000000017369676E696E67206B65790000000100 '` to a `[byte[]]` array. If so, [this answer](https://stackoverflow.com/a/54543794/45375) should help. If it does, we can close your question as a duplicate. – mklement0 Nov 25 '22 at 21:45

0 Answers0