Skip to content

Commit

Permalink
Fix filenames with spaces breaking SHA256 parsing
Browse files Browse the repository at this point in the history
Bit of magic numbers here: SHA256 hashes are _always_ 256 bits. 256/4
yields 64 nybbles. Remove the trailing newline, grab the last 64
characters, then the first 7 for the filename hash.
  • Loading branch information
Pinjasaur committed Apr 8, 2019
1 parent 51eee47 commit f55479e
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions bin/optimg
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ rename_images() {
if file "${file}" | grep -qE 'image|bitmap' && [[ ! -d "${file}" ]]; then

# First 7 characters of SHA2 hash
hash="$(openssl sha256 "${file}" | awk '{print $2}' | head -c 7)"
hash="$(openssl sha256 "${file}" | tr -d '\n' | tail -c 64 | head -c 7)"
full="$(basename "${file}")"
# Get extension (... and convert jpeg -> jpg)
ext="$(echo "${full##*.}" | sed -e 's/e//g')"
Expand All @@ -83,7 +83,7 @@ optimize_images() {
if file "${file}" | grep -qE 'image|bitmap' && [[ ! -d "${file}" ]]; then

# First 7 characters of SHA2 hash
hash="$(openssl sha256 "${file}" | awk '{print $2}' | head -c 7)"
hash="$(openssl sha256 "${file}" | tr -d '\n' | tail -c 64 | head -c 7)"
full="$(basename "${file}")"
# Get extension (... and convert jpeg -> jpg)
ext="$(echo "${full##*.}" | sed -e 's/e//g')"
Expand Down

0 comments on commit f55479e

Please sign in to comment.