David Beattie
David Beattie

Reputation: 11

Base64 encoding in Powershell

I am trying to base64 encode the sha1 hash on a list of files, being read from a REST platform. The read back of the files works fine and in fact the whole script works fine, however the problem is that I can only work out how to Base64 encode a string representation of the SHA1 hash, however I need to B64 encode the actual hash, not the string of the hash.

Below is some of the code I am using but again I don't want to B64 encode the string of the hash but the hash itself.

As an example, the output of the code I have for a file is below, the first is the hash calculated by another system which B64 encodes the raw hash and the second is the hash created by our code, B64 encoding the string value of the hash which are completely different.

Any help would be appreciated.

Source file: /rest/CCLAIMS/053/17667053 AERTRMT1xZNrW9TTl6k6Orryiwc12gtJQfJSnlOeWGI=

Target file hash /rest/CCLAIMS/053/17667053 M0U1NDY0NDk1NEJDNjVBRTNEMEU3M0JBNTkyNzk4QzMwQ0M3MEU2NA==

Function Get-StringHash([String] $String,$HashName = "MD5") { 
    $StringBuilder = New-Object System.Text.StringBuilder 
    [System.Security.Cryptography.HashAlgorithm]::Create($HashName).ComputeHash(
        [System.Text.Encoding]::UTF8.GetBytes($String)
    )|%{ 
        [Void]$StringBuilder.Append($_.ToString("x2")) 
    } 
    $StringBuilder.ToString() 
}

$hash = "SHA1"

$filehash = Get-FileHash -Path C:\Temp\PS\output.file -Algorithm $hash
$hashvalue = [system.text.encoding]::UTF8.GetBytes($filehash.Hash)
    add-content $outputfile ($line + "," + $filehash.Hash + "," + [system.convert]::ToBase64String($hashvalue))
} Catch {
    $errormessage = $_.Exception.Message
    add-content $outputerrorfile ($line + "," + "Error "+$errormessage)
}     

Upvotes: 1

Views: 6237

Answers (1)

A. Wilson
A. Wilson

Reputation: 8840

Something about your code is malformatted, so I'm not entirely sure what your original intention was there. Anyway, here is a naive conversion to byte array from a hex string (using https://cyber-defense.sans.org/blog/2010/02/11/powershell-byte-array-hex-convert as a template):

function Naive-Convert-HashToByteArray {
    [CmdletBinding()]
    Param ( [Parameter(Mandatory = $True, ValueFromPipeline = $True)] [String] $String )
    $String -split '([A-F0-9]{2})' | foreach-object { if ($_) {[System.Convert]::ToByte($_,16)}}
}

then

$filehash = Get-FileHash -Path C:\Temp\PS\output.file -Algorithm $hash
$filehash_as_bytes = Naive-Convert-HashToByteArray($filehash.Hash)
[System.Convert]::ToBase64String($filehash_as_bytes)

should produce the result encoding you're looking for. You should be able to concat that to the thing you want and write it to a file from there.

("Naive" in the name there means it's assuming the incoming string is longer than a single character and in the particular format that the .Hash attribute is from that object, which seemed a reasonable assumption)

Upvotes: 3

Related Questions