首页 > 解决方案 > 比较 Azure Properties.ContentMD5 和 Get-Filehash 之间的字符串输出

问题描述

标签: powershellencodinghashmd5azure-blob-storage

解决方案


ContentMD5 is a base64 representation of the binary hash value, not the resulting hex string :)

$md5sum = [convert]::FromBase64String('Z78raj5mVwVLS4bhN6Ejgg==')
$hdhash = [BitConverter]::ToString($md5sum).Replace('-','')

Here we convert base64 -> binary -> hexadecimal


If you need to do it the other way around (ie. for obtaining a local file hash, then using that to search for blobs in Azure), you'll first need to split the hexadecimal string into byte-size chunks, then convert the resulting byte array to base64:

$hdhash = '67BF2B6A3E6657054B4B86E137A12382'
$bytes  = [byte[]]::new($hdhash.Length / 2)
for($i = 0; $i -lt $bytes.Length; $i++){
  $offset = $i * 2
  $bytes[$i] = [convert]::ToByte($hdhash.Substring($offset,2), 16)
}
$md5sum = [convert]::ToBase64String($bytes)

 


推荐阅读