From 7bb043aa26cd771e1ebf7fb824182a905de5a9f6 Mon Sep 17 00:00:00 2001 From: CyberLeo Date: Sun, 20 Oct 2013 01:21:02 -0500 Subject: [PATCH] Rework crc32 algorithm selection mechanism --- flag | 83 ++++++++++++++++++++++-------------------------------------- 1 file changed, 30 insertions(+), 53 deletions(-) diff --git a/flag b/flag index 587f4fe..38cbd4a 100755 --- a/flag +++ b/flag @@ -8,63 +8,40 @@ # $HOME/.flag <- Local user's flag # Check them in that order. -hash_string() { - # Hash the passed string into 8 hex characters - [ -z "${crc32_bin}" ] && find_crc32_bin - if [ -z "${crc32_bin}" ] - then - echo "No crc32 algo found!" >&2 - echo "deadbeef" - else - echo -n "${*}" | eval "${crc32_bin}" | awk '{print $1}' - fi -} - -find_crc32_bin() { - crc32="" - for bin in crc32sum crc32sum-linux crc32sum-freebsd +# Name available algos as algo_something() and left-align for autodetection +# Order the algos by priority: the first one that is correct will be used +algo_cksum_linux() { printf '%08x\n' $(cksum) | head -n 1; } +algo_cksum_freebsd() { printf '%08x\n' $(cksum -o 3) | head -n 1; } +algo_crc32() { crc32 /dev/stdin; } +algo_crc32sum() { crc32sum; } +algo_crc32sum_linux() { crc32sum-linux; } +algo_crc32sum_freebsd() { crc32sum-freebsd; } +algo_ruby() { ruby -e 'require "zlib"; printf "%08x\n", Zlib.crc32(STDIN.readlines.join)'; } +algo_php() { php -r 'printf("%08x\n", crc32(file_get_contents("/dev/stdin")));'; } +algo_perl() { perl -e 'use String::CRC32; printf("%08x\n", crc32(*STDIN));'; } + +find_crc32_algo() { + sed -e '/^algo_[^(]*()/!d; s/().*$//' "${0}" | while read algo do - bin="$(which "${bin}" 2>/dev/null)" - if [ -x "${bin}" ] + if [ "$(printf "meow" | "${algo}" 2>/dev/null)" = "8a106afe" ] then - export crc32_bin="${bin}" - if [ "$(hash_string "meow" 2>/dev/null)" = "8a106afe" ] - then - crc32="${bin}" - break - fi + echo "${algo}" + break fi done +} - if [ -z "${crc32}" ] - then - for bin in cksum ruby php perl - do - bin="$(which "${bin}" 2>/dev/null)" - if [ -x "${bin}" ] - then - case "${bin}" in - */cksum) bin="printf '%08x\n' \$(${bin} -o 3) | head -n 1" ;; - */ruby) bin="${bin} -e 'require \"zlib\"; printf(\"%08x\n\", Zlib.crc32(STDIN.readlines.join))'" ;; - */php) bin="${bin} -r 'printf(\"%08x\n\", crc32(file_get_contents(\"/dev/stdin\")));'" ;; - */perl) bin="${bin} -e 'use String::CRC32; printf(\"%08x\n\", crc32(*STDIN));'" ;; - esac - export crc32_bin="${bin}" - if [ "$(hash_string "meow" 2>/dev/null)" = "8a106afe" ] - then - crc32="${bin}" - break - fi - fi - done - fi - if [ -n "${crc32}" ] +hash_string() { + # Hash the passed string into 8 hex characters + [ "${crc32_algo}" ] || crc32_algo="$(find_crc32_algo)" + if [ -z "${crc32_algo}" ] then - export crc32_bin="${crc32}" - else - echo "Failed to locate usable crc32 algo" >&2 - unset crc32_bin + echo "No crc32 algo found!" >&2 + echo "deadbeef" return 1 + else + echo -n "${*}" | "${crc32_algo}" 2>/dev/null | awk '{print $1}' + return 0 fi } @@ -77,9 +54,9 @@ flagcache(){ fi if [ -z "${flag_hex}" ] then - flag_hex="$(hash_string "${myhost}")" - [ "${crc32_bin}" ] && echo -e "${myhost}\t${flag_hex}" >> "${cache}" + flag_hex="$(hash_string "${myhost}")" && echo -e "${myhost}\t${flag_hex}" >> "${cache}" fi + echo "${flag_hex}" } if [ -f "${HOME}/.flag" ] @@ -94,7 +71,7 @@ fi if [ -z "${flag_hex}" ] then - flagcache + flag_hex="$(flagcache)" fi # Now we definitely have flag_hex -- 2.42.0