slothsoffun
2/28/2018 - 6:54 PM

Hash Algorithms and Encoding in Unix

# MD5
md5 $file
echo -n “test123” | md5

# SHA
shasum -a 1 $file

# -a(lgorithm) 1 (default), 224, 256, 384, 512, 512224, 512256

# -c(heck) $checksum-file -> contains hash and filename
shasum -a 256 -c ubuntu-check.lst


## OPENSSL
openssl sha1 path-to-the-file
openssl ... path-to-the-file

# Check file type (extension, magic header, content, language)
file unknown.ext

# get first 12 bytes of file
head -c 12 unknown.ext | xxd
head -c 12 unknown.ext | xxd -p
head -c 12 unknown.ext | hexdump

# xxd (hexdump tool)
xxd binary.bin
xxd -p # continous hexdump (no columns)
xxd -r # revert = take hexdump and put out binary
xxd -u # uppercase letters for hex

# Base64 #
base64 binary.bin
echo -n "SGVsbG8gV29ybGQK" | base64 -d

## PLAIN -> SHA2 -> HEX (bin) -> B64
echo -n abc123 | sha256sum | xxd -r -p | base64

# get random bytes
dd if=/dev/urandom count=32 bs=1 2> /dev/null | base64