开发者

Convert Perl MD5 Digest and Create to PowerShell

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: M开发者_JS百科D5


Path       :
HashString : 04BF4CA4BF3E34C83F0B11970205580D


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜