Basha256.sh – Pure Bash 3.2 implementation of sha256

ozkatz1 pts0 comments

A pure-bash implementation of sha256. Why? because we can. · GitHub

/" data-turbo-transient="true" />

Skip to content

-->

Search Gists

Search Gists

Sign in

Sign up

You signed in with another tab or window. Reload to refresh your session.<br>You signed out in another tab or window. Reload to refresh your session.<br>You switched accounts on another tab or window. Reload to refresh your session.

Dismiss alert

{{ message }}

Instantly share code, notes, and snippets.

ozkatz/basha256.sh

Created<br>June 29, 2026 22:35

Show Gist options

Download ZIP

Star

(0)

You must be signed in to star a gist

Fork

(0)

You must be signed in to fork a gist

Embed

Select an option

Embed<br>Embed this gist in your website.

Share<br>Copy sharable link for this gist.

Clone via HTTPS<br>Clone using the web URL.

No results found

Learn more about clone URLs

Clone this repository at &lt;script src=&quot;https://gist.github.com/ozkatz/dc7606ea68138b75999cbb1b271072a6.js&quot;&gt;&lt;/script&gt;

" readonly="readonly" data-autoselect="true" data-target="primer-text-field.inputElement " aria-describedby="validation-0f34a4ab-2139-4079-a728-5661d454a9af" class="form-control FormControl-monospace FormControl-input FormControl-small rounded-left-0 rounded-right-0 border-right-0" type="text" name="gist-share-url-sized-down" />

Save ozkatz/dc7606ea68138b75999cbb1b271072a6 to your computer and use it in GitHub Desktop.

Embed

Select an option

Embed<br>Embed this gist in your website.

Share<br>Copy sharable link for this gist.

Clone via HTTPS<br>Clone using the web URL.

No results found

Learn more about clone URLs

Clone this repository at &lt;script src=&quot;https://gist.github.com/ozkatz/dc7606ea68138b75999cbb1b271072a6.js&quot;&gt;&lt;/script&gt;

" readonly="readonly" data-autoselect="true" data-target="primer-text-field.inputElement " aria-describedby="validation-61e0c9a2-4e47-4505-8fd4-e0b75b383f72" class="form-control FormControl-monospace FormControl-input FormControl-small rounded-left-0 rounded-right-0 border-right-0" type="text" name="gist-share-url-original" />

Save ozkatz/dc7606ea68138b75999cbb1b271072a6 to your computer and use it in GitHub Desktop.

Download ZIP

A pure-bash implementation of sha256. Why? because we can.

Raw

basha256.sh

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br>Learn more about bidirectional Unicode characters

Show hidden characters

#!/usr/bin/env bash

# basha256.sh - sha256 in pure Bash 3.2

# ./basha256.sh

# Everything below: the message schedule, the compression function and even

# reading the raw bytes off stdin is done with nothing but Bash builtins and arithmetic.

# TIL: Bash cannot store a NUL byte in a variable, so stdin is read in NUL-delimited chunks and the delimiters are re-inserted by hand.

# LC_ALL=C makes every byte a single "character" so slicing works.

# the 512-bit block compression function

# Args: the sixteen big-endian 32-bit words of one block.

# Reads/updates the eight hash words _H0.._H7 of the calling scope.

_sha256_compress() {

local M=4294967295 # 0xffffffff, the 32-bit mask

local -a w=( "$@" ) # w[0..15] = this block's words

local -a K=(

0x428a2f98 0x71374491 0xb5c0fbcf 0xe9b5dba5 0x3956c25b 0x59f111f1 0x923f82a4 0xab1c5ed5

0xd807aa98 0x12835b01 0x243185be 0x550c7dc3 0x72be5d74 0x80deb1fe 0x9bdc06a7 0xc19bf174

0xe49b69c1 0xefbe4786 0x0fc19dc6 0x240ca1cc 0x2de92c6f 0x4a7484aa 0x5cb0a9dc 0x76f988da

0x983e5152 0xa831c66d 0xb00327c8 0xbf597fc7 0xc6e00bf3 0xd5a79147 0x06ca6351 0x14292967

0x27b70a85 0x2e1b2138 0x4d2c6dfc 0x53380d13 0x650a7354 0x766a0abb 0x81c2c92e 0x92722c85

0xa2bfe8a1 0xa81a664b 0xc24b8b70 0xc76c51a3 0xd192e819 0xd6990624 0xf40e3585 0x106aa070

0x19a4c116 0x1e376c08 0x2748774c 0x34b0bcb5 0x391c0cb3 0x4ed8aa4a 0x5b9cca4f 0x682e6ff3

0x748f82ee 0x78a5636f 0x84c87814 0x8cc70208 0x90befffa 0xa4506ceb 0xbef9a3f7 0xc67178f2

local i x y s0 s1 S0 S1 ch maj t1 t2 a b c d e f g h

# Extend the sixteen words into sixty-four. rotr(v,n) = (v>>n)|(v

# masked to 32 bits after the XORs.

for (( i=16; i64; i++ )); do

x=${w[i-15]}; y=${w[i-2]}

s0=$(( ( ((x>>7)|(x25)) ^ ((x>>18)|(x14)) ^ (x>>3) ) & M ))

s1=$(( ( ((y>>17)|(y15)) ^ ((y>>19)|(y13)) ^ (y>>10) ) & M ))

w[i]=$(( (w[i-16] + s0 + w[i-7] + s1) & M ))

done

a=$_H0; b=$_H1; c=$_H2; d=$_H3; e=$_H4; f=$_H5; g=$_H6; h=$_H7

# this is pretty and was fun to debug:

for (( i=0; i64; i++ )); do

S1=$(( ( ((e>>6)|(e26)) ^ ((e>>11)|(e21)) ^ ((e>>25)|(e7)) ) & M ))

ch=$(( (e & f) ^ ((~e) & g) ))

t1=$(( (h + S1 + ch + K[i] + w[i]) & M ))

S0=$(( ( ((a>>2)|(a30)) ^ ((a>>13)|(a19)) ^ ((a>>22)|(a10)) ) & M ))

maj=$(( (a & b) ^ (a & c) ^ (b & c) ))

t2=$(( (S0 + maj) & M ))

h=$g; g=$f; f=$e; e=$(( (d + t1) & M )); d=$c; c=$b; b=$a; a=$(( (t1 + t2) & M ))

done

_H0=$(( (_H0 + a) & M )); _H1=$(( (_H1 + b) & M ))

_H2=$(( (_H2 + c) & M )); _H3=$((...

gist clone bash embed formcontrol basha256

Related Articles