views:

71

answers:

2

I am trying to convert the following in Perl to PowerShell, I am stuck on the MD5 Digest and Create.

timestamp=1283473470
key='this-is-my-key'
secret='secret'
perl -e  "use Digest::MD5 qw(md5 md5_hex); print md5_hex('$key' . '$secret' . $timestamp);"

For testing purposes I am setting the time stamp to a static number. That way I can compare what Perl says and what PowerShell says. I'v tried out a few MD5 and [System.Security.Cryptography.HashAlgorithm] attempts but so far I have manged to little more than confuse myself.

In Perl....

> perl -e  "use Digest::MD5 qw(md5 md5_hex); print md5_hex('this-is-my-key' . 'secret' . '1283473470');"
> a135923fb8e579463f312b69528d243c

In PowerShell

>_ 'this-is-my-key.secret.1283473470' | Get-Hash


Algorithm: MD5


Path       :
HashString : 04BF4CA4BF3E34C83F0B11970205580D
+2  A: 

There is a Get-Hash cmdlet in the PowerShell Community Extensions. Give it a try.

PS> 'this-is-my-key.secret.1283473470' | Get-Hash


Algorithm: MD5


Path       :
HashString : 04BF4CA4BF3E34C83F0B11970205580D

or if the string needs to be interpreted as ASCII:

PS> $foo = 'THIS-is-my-keysecret1283473470'
PS> $foo.ToLower() | Get-Hash -StringEncoding ascii


Algorithm: MD5


Path       :
HashString : A135923FB8E579463F312B69528D243C
Keith Hill
lol I was throwing all sorts of things at get-hash and it never occurred to me to pipe the string to it. trying now.
Aaron Wurthmann
Darn! I was so hopeful. They don't match. Perl gives: a135923fb8e579463f312b69528d243c
Aaron Wurthmann
What does the perl dot syntax `'str' . 'str'` do here?
Keith Hill
Looks like they do a combine. So the command should be:'this-is-my-keysecret1283473470' | Get-Hash -StringEncoding ascii
Aaron Wurthmann
Hmm.. can't seem to pipe variables to Get-hash... also looks like I'll have to convert to lowercase.
Aaron Wurthmann
Hmm, works for me - see updated answer. How are you attempting to pipe the variable?
Keith Hill
Got it! Thanks Keith you are the man.. again!
Aaron Wurthmann
A: 

Alright here is is, in large part due to Keith Hill's direction. Mind you in this case I found it best to specify [string] everywhere, this is more or less just based on being consistent it only needs to be specified in two of the lines.

[string]$timestamp=1283473470
[string]$key='this-is-my-key'
[string]$secret='secret'
[string]$string=$key+$secret+$timestamp
[string]$CAPhash=$string | Get-Hash -StringEncoding ascii
[string]$hash=$CAPhash.ToLower()
$hash

I'm not sure if there is a way to combine the last two lines into one. For the moment I am pleased with what I have.

Aaron Wurthmann