Skip to content

Commit

Permalink
include modules hash in golden hash, windows
Browse files Browse the repository at this point in the history
  • Loading branch information
artoonie committed Feb 23, 2024
1 parent c5f4963 commit 24313bd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
31 changes: 31 additions & 0 deletions .github/actions/Sha-Of-Directory.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## Creates a SHA of all files in the directory but ignores their metadata
## This is useful when you want to ignore all timestamps of a zip, for example:
## You can extract the contents to a directory, then run this.
## Usage: powershell Sha-Of-Directory.ps1 <DIRECTORY> <sha version: 1, 256, or 512>

$Directory=$args[0]
$ShaA=$args[1]

# First, create a hash algorithm object using SHA256.
$Algorithm = [System.Security.Cryptography.HashAlgorithm]::Create("SHA" + $ShaA)

# Next, create a cryptographic stream of data using the SHA256 hash algorithm.
$CryptoStream = [System.Security.Cryptography.CryptoStream]::new(
([System.IO.Stream]::Null),
$Algorithm,
"Write"
)

# Retrieve each file and copy the data into the cryptographic stream.
echo $Directory
foreach ($File in Get-ChildItem -Recurse -Path $Directory -File) {
# Write-Host $File
$FileStream = [io.file]::OpenRead($File.FullName)
$FileStream.CopyTo($CryptoStream)
}

# Close all files and close out the cryptographic stream.
$CryptoStream.FlushFinalBlock()

# Combine all of the hashes as hexadecimal formats "X2" and join the values.
($Algorithm.Hash | ForEach-Object {$_.ToString("X2")}) -join ''
28 changes: 19 additions & 9 deletions .github/actions/sha-of-zip.bat
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ set SHA_A=%2

setlocal EnableExtensions EnableDelayedExpansion

:: All paths relative to pwd -- where this script is called from
set "EXTRACTIONDIR=.\rcv\zip_extracted"
set "MODULESFILE=.\rcv\zip_extracted\rcv\lib\modules"
set "MODULESDIR=.\rcv\zip_extracted\rcv\lib\modules_extracted"
set "HASHFILE_UNSORTED=all_hashes_unsorted.txt"
set "HASHFILE_PATH_STRIPPED=all_hashes_path_stripped.txt"
set "HASHFILE_SORTED=all_hashes_sorted.txt"
set "EXTRACTIONDIR=.\rcv\zip_extracted"
set "MODULESFILE=.\rcv\lib\modules"

if exist %HASHFILE_UNSORTED% (
del %HASHFILE_UNSORTED%
Expand All @@ -23,20 +25,28 @@ if exist %EXTRACTIONDIR% (
rmdir /s /q %EXTRACTIONDIR%
)

mkdir %EXTRACTIONDIR%
powershell -command Expand-Archive -Path %ZIP_FILEPATH% -Destination %EXTRACTIONDIR%
cd %EXTRACTIONDIR%
if exist %MODULESDIR% (
rmdir /s /q %EXTRACTIONDIR%
)

:: Remove modules file, which doesn't vary on the same machine but does vary across machines
powershell -command Expand-Archive -Path %ZIP_FILEPATH% -Destination %EXTRACTIONDIR%

:: Extract modules, get the SHA-512 of it, and add it to the top of the hashfile
jimage extract --dir %MODULESDIR% %MODULESFILE%

powershell .github\actions\Sha-Of-Directory.ps1 %MODULESDIR% 512 > %HASHFILE_UNSORTED%

:: Delete both modules and the extracted dir
del %MODULESFILE%
rmdir /s /q %MODULESDIR%

:: Calculate the hash for every file here and in all subdirectories, appending to the file (format "(filename) = (hash)")
(
for /r . %%f in (*) do (
for /r %EXTRACTIONDIR% %%f in (*) do (
<NUL set /p ="%%f = "
C:\Windows\System32\certutil.exe -hashfile "%%f" SHA%SHA_A% | findstr /v ":"
)
) > %HASHFILE_UNSORTED%
) >> %HASHFILE_UNSORTED%

:: Replace the absolute paths to each file with relative paths (e.g. C:\temp\rcv => .\rcv)
set "SEARCHTEXT=%cd%"
Expand All @@ -52,7 +62,7 @@ sort "%HASHFILE_PATH_STRIPPED%" > "%HASHFILE_SORTED%"
C:\Windows\System32\certutil.exe -hashfile %HASHFILE_SORTED% SHA%SHA_A% | findstr /v ":"

:: For debugging, enable printing the file-by-file hash
:: echo "File-by-file hash"
:: echo File-by-file hash
:: type "%HASHFILE_SORTED%"

endlocal
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ jobs:
name: Package
if-no-files-found: error
path: |
${{ github.workspace }}/all_hashes_sorted.txt
${{ github.workspace }}/${{ steps.zipfn.outputs.FILEPATH }}
${{ github.workspace }}/${{ steps.zipfn.outputs.FILEPATH }}.sha512
${{ github.workspace }}/${{ steps.zipfn.outputs.FILEPATH }}.golden.sha512
Expand Down

0 comments on commit 24313bd

Please sign in to comment.