Skein-Bash code

A very useful bash script. Thank you.
Matt Tomasello’s blog, new but will look back in the future.

#!/bin/bash

#
# Author: Matt Tomasello, 2011
# Based on the JavaScript implementation written by Thomas Mueller, 2008-2010
# which was based on the C reference implementation written by Doug Whitting,
# 2008. Special thanks to Mike Drob for the zero-fill right-shift algorithm.
# This algorithm and source code is released to the public domain.
#

# 64-bit architecture check
declare ARCH=0
for (( i=0; i&2
	fi
}

# Displays usage info
function usage {
	echo 'Author: Matt Tomasello, 2011'
	echo 'Based on the JavaScript implementation written by Thomas Mueller, 2008-2010'
	echo 'which was based on the C reference implementation written by Doug Whitting,'
	echo '2008. Special thanks to Mike Drob for the zero-fill right-shift algorithm.'
	echo
	echo 'This algorithm and source code is released to the public domain.'
	echo
	echo 'This program will calculate the Skein 512-512 hash of STDIN using version 1.3 of'
	echo 'the Skein algorithm.'
	echo
	echo 'Usage: cat FILE | skein [ARGS]'
	echo "       echo 'some-string' | skein [ARGS]"
	echo
	echo 'Valid arguments may start with one or two hyphens, and include:'
	echo '  -h, -help     Display this help information'
	echo '  -selftest     Perform self-test'
	echo '  -debug        Display short debug information on STDERR'
	echo
	exit
}

# Converts hex-string like '62FF0ACC' into ASCII string
function hex2str {
	HEX2STR=''
	if [[ -n "$1" && ${#1}%2 -eq 0 ]]; then
		for (( i=0; i> $2) & (N>>~-$2) ))
	fi
}

function block {
	local offset=${1:-0}
	local b=("${!2}")
	local nil=${3:-0}
	local x=()
	local t=()
	if [[ "$nil" -eq 1 ]]; then
		unset b
	fi
	C[16]=466688986
	C[17]=2851871266
	for (( i=0; i=0; j--,k-- )); do
			shift_left ${t[i*2]} ${t[i*2+1]} 8
			t[i*2]=${SL[0]}
			bk=$(( b[k] & 255 ))
			t[i*2+1]=$(( SL[1]|(bk&255) ))
		done
		add t[i*2] t[i*2+1] C[i*2] C[i*2+1]
		x[i*2]=${ADD[0]}
		x[i*2+1]=${ADD[1]}
		xor ${C[16]} ${C[17]} ${C[i*2]} ${C[i*2+1]}
		C[16]=${XOR[0]}
		C[17]=${XOR[1]}
	done
	add x[10] x[11] TWEAK[0] TWEAK[1]
	x[10]=${ADD[0]}
	x[11]=${ADD[1]}
	add x[12] x[13] TWEAK[2] TWEAK[3]
	x[12]=${ADD[0]}
	x[13]=${ADD[1]}
	xor ${TWEAK[0]} ${TWEAK[1]} ${TWEAK[2]} ${TWEAK[3]}
	TWEAK[4]=${XOR[0]}
	TWEAK[5]=${XOR[1]}
	for (( round=1; round>2))&3) ))
			local n=$(( (1+i+i)&7 ))
			local r0=${R[p+i]}
			add x[m*2] x[m*2+1] x[n*2] x[n*2+1]
			x[m*2]=${ADD[0]}
			x[m*2+1]=${ADD[1]}
			shift_left ${x[n*2]} ${x[n*2+1]} r0
			shift_right ${x[n*2]} ${x[n*2+1]} $(( 64-r0 ))
			xor ${SL[0]} ${SL[1]} ${SR[0]} ${SR[1]}
			x[n*2]=${XOR[0]}
			x[n*2+1]=${XOR[1]}
			xor ${x[n*2]} ${x[n*2+1]} ${x[m*2]} ${x[m*2+1]}
			x[n*2]=${XOR[0]}
			x[n*2+1]=${XOR[1]}
		done
		for (( i=0; i64; len-=64,pos+=64 )); do
		TWEAK[1]=$(( TWEAK[1]+64 ))
		block pos STR2BYTES[@]
		TWEAK[2]=805306368
	done
	TWEAK[1]=$(( TWEAK[1]+len ))
	TWEAK[2]=$(( TWEAK[2]|2147483648 ))
	block pos STR2BYTES[@]
	TWEAK[1]=8
	TWEAK[2]=4278190080
	block 0 0 1
	HASH=()
	for (( i=0; i>3 ))
		shift_right ${C[is3*2]} ${C[is3*2+1]} $(( (i&7)*8 ))
		local b=$(( (SR[1]&255)+256 ))
		HASH[i]=$b
	done
	bytes2hex HASH[@]
	echo $BYTES2HEX
}

# Checks that all functions operate correctly
function selftest {
	PASS=1
	local hex='666f6f626172'
	local str='foobar'
	local bytes=(102 111 111 98 97 114)
	local chk=0
	local msg=''

	msg='Testing hex2str:'
	hex2str "$hex"
	if [[ "$HEX2STR" = "$str" ]]; then
		msg="$msg Success"
	else
		PASS=0
		msg="$msg Failed with ${HEX2STR[@]}"
	fi
	echo "$msg"

	msg='Testing str2hex:'
	str2hex "$str"
	if [[ "$STR2HEX" = "$hex" ]]; then
		msg="$msg Success"
	else
		PASS=0
		msg="$msg Failed with ${STR2HEX[@]}"
	fi
	echo "$msg"

	msg='Testing hex2bytes:'
	hex2bytes "$hex"
	chk=1
	for (( i=0; i
This entry was posted in Fun, Software. Bookmark the permalink.

Comments are closed.