将md5校验和从Powershell转换为Shell脚本

时间:2017-12-10 22:01:08

标签: shell powershell

我在Powershell中有一个工作脚本:

function md5($text){
  $md5 = New-Object System.Security.Cryptography.MD5CryptoServiceProvider
  $md5.ComputeHash([Text.Encoding]::utf8.getbytes($text))|%{$HC=''}{$HC+=$_.tostring("x2")}{$HC}
}

$Password="D e o t 2 3 3 S w i t c h"
$Challenge="6 1 7 e 3 d 3 f"
$Code1=$Challenge+"-"+$Password 
echo $Code1
# gives "617e3d3f-Deot233Switch"
$Code2=[char[]]$Code1|%{$Code2=""}{$Code2+=$_+[Char]0}{$Code2}
echo $Code2
# gives "6 1 7 e 3 d 3 f - D e o t 2 3 3 S w i t c h"
# I dont know how to do that in Shell
$Response= $(md5($Code2))
echo $Response
# gives "7a856cb4a061ec00ffdf9bca23caffa2"
# I this is the correct calculated Value!!!!!

现在我必须在shell中“翻译”这个脚本(在synology diskstation上工作)但是计算结果不匹配,我需要帮助。

我试过了:

#!/bin/sh


Password = "D e o t 2 3 3 S w i t c h"
Challenge = "6 1 7 e 3 d 3 f"
    hash3=$(echo -n "$Challenge-$Password" |sed -e 's,.,&\n,g' | tr '\n' '\0' | md5sum | grep -o "[0-9a-z]\{32\}")
    echo "[hash32]$hash3" #[hash32]494e6cec7483a4ee0938895519a84bc7
    hash4=$(echo -n "$Challenge-$Password" | md5sum )
    echo "[hash4]$hash4" #[hash4]336d5ebc5436534e61d16e63ddfca327  -
    hash5=$(echo -n "$Challenge-$Password" | md5sum | grep -o "[0-9a-z]\{32\}")
    echo "[hash5]$hash5" #[hash5]336d5ebc5436534e61d16e63ddfca327
    hash6=$(echo -n "$Challenge-$Password" |sed -e 's,.,&\n,g'| md5sum | grep -o "[0-9a-z]\{32\}")
    echo "[hash6]$hash6" # [hash6]0a1f21a3417389e0c0a13392c79a7a89
# the "sed -e 's,.,&\n,g'" maybe the key?

我真的不明白这些选项,只知道md5产生的不匹配。

也许有人看到错误,谢谢

0 个答案:

没有答案