Skip to content

Commit

Permalink
hash.sh script
Browse files Browse the repository at this point in the history
  • Loading branch information
1zun4 committed Mar 12, 2024
1 parent a55b19b commit 0763eb2
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions hash.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash

# Function to create SHA256 hash for a file
create_sha256_file() {
sha256sum "$1" | cut -d ' ' -f 1
}

# Function to update .tar.gz files with .hash and .sha256
update_tar_gz() {
local input_file="$1"
local output_folder="./output"
local output_file="$output_folder/$(basename "$input_file")"

# Create a temporary directory for extraction
local tmp_dir="$(mktemp -d)"

# Extract files from .tar.gz
tar -xzf "$input_file" -C "$tmp_dir"

# Create a .hash file with SHA256 hashes
jq -n '{}' > "$tmp_dir/.hash"
find "$tmp_dir" -type f | while read -r file; do
relative_path="${file#$tmp_dir/}"
if [ "$relative_path" != ".hash" ]; then
hash_value=$(create_sha256_file "$file")
jq --arg file "$relative_path" --arg hash "$hash_value" '.[$file] = $hash' "$tmp_dir/.hash" > "$tmp_dir/.hash_tmp" && mv "$tmp_dir/.hash_tmp" "$tmp_dir/.hash"
fi
done


# Create a new compressed .tar file
tar -czf "$output_file" -C "$tmp_dir" .

# Clean up temporary directory
rm -rf "$tmp_dir"

# Create a .sha256 file for the updated .tar.gz
sha256_hash=$(create_sha256_file "$output_file")
echo "$sha256_hash" > "$output_folder/$(basename "$input_file").sha256"
}

# Process all .tar.gz files in the current directory
for file_name in *.tar.gz; do
if [ -f "$file_name" ]; then
update_tar_gz "$file_name"
echo "Processing completed for $file_name"
fi
done

0 comments on commit 0763eb2

Please sign in to comment.