From 05238c95564603ab48f9266d84d43f028dd360af Mon Sep 17 00:00:00 2001 From: Ian Mancini Date: Wed, 16 Feb 2022 23:13:33 -0300 Subject: [PATCH] Add compare_checksum function to raspi-flash --- raspi-flash | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/raspi-flash b/raspi-flash index fd38624..dfaef56 100755 --- a/raspi-flash +++ b/raspi-flash @@ -37,6 +37,17 @@ download() { else exit 1; fi } +compare_checksum() { + echo "Checking sha256 hashes" + sum=$(sha256sum "$1" | cut -f 1 -d ' ') + if [ "$sum" = "$2" ]; then + echo "Checksum matches" + else + echo "Checksum doesn't match. Remove file and download again" + exit 1 + fi +} + extract() { printf "Will extract %s in %s\nExtracted size: %s\nAvailable space: %s\n\n" \ "$1" "$DOWNLOADS_DIR" "$2" "$(check_available_space)" @@ -44,16 +55,8 @@ extract() { read -rp "Continue? [Y/n]" continue continue=${continue:-Y} if [[ $continue =~ [yY] ]]; then - echo "Extracting..." - unzip "$DOWNLOADS_DIR/$1" -d "$DOWNLOADS_DIR" - echo "Checking sha256 hashes" - sum=$(sha256sum "$3" | cut -f 1 -d ' ') - if [ "$sum" = "$4" ]; then - echo "Checksum matches" - else - echo "Checksum doesn't match. Remove file and download again" - exit 1 - fi + echo "Extracting..." + unzip "$DOWNLOADS_DIR/$1" -d "$DOWNLOADS_DIR" else exit 1; fi } @@ -108,7 +111,8 @@ else fi [ $should_download = TRUE ] && download "$ZIP_FILENAME" "${SELECTION_DATA[3]}M" "${SELECTION_DATA[2]}" -[ $should_extract = TRUE ] && extract "$ZIP_FILENAME" "${SELECTION_DATA[4]}M" "$IMG_FILENAME" "${SELECTION_DATA[5]}" +[ $should_extract = TRUE ] && extract "$ZIP_FILENAME" "${SELECTION_DATA[4]}M" +compare_checksum "$DOWNLOADS_DIR/$IMG_FILENAME" "${SELECTION_DATA[5]}" # NOTE: Is there a cleaner way to do this? DEVICES=$(list_removable_devices)