THAMER
Sovereign Bitcoin Wallet

WALLET AI BITCOIN

Your Bitcoin Wallet. Your Keys. Your Engine.

Self-hosted Bitcoin software built for developers, applications, and AI agents.

CREATE WALLET

SHA-256 and Double SHA-256 in Go

permalink THAMER
#bitcoin#bitcoin-development#bitcoin-script#golang

GOLANG + BITCOIN SCRIPT — Stage 001, Lesson 08 of 12.

Concept

SHA-256 maps arbitrary bytes to a 32-byte digest. Bitcoin uses double SHA-256 in several contexts, including legacy transaction IDs and block header hashes. Taproot uses tagged SHA-256 in specific contexts instead, so double hashing is not a universal Bitcoin rule.

Engineering practice

Hash the original bytes, then hash the first digest bytes, not their hexadecimal text. The example uses an empty input and compares the result with a fixed known digest. A displayed transaction ID often reverses the digest byte order; that presentation is separate from hashing.

Source example

Go · 9 lines
1package main
2
3import (
4	"crypto/sha256"
5	"fmt"
6)
7
8func doubleHash(b []byte) [32]byte { h := sha256.Sum256(b); return sha256.Sum256(h[:]) }
9func main()                        { fmt.Printf("%x\n", doubleHash(nil)) }

Run locally

From the 001-foundations directory:

go test ./04-sha256
go run ./04-sha256

Regression test

Go · 12 lines
1package main
2
3import (
4	"fmt"
5	"testing"
6)
7
8func TestExample(t *testing.T) {
9	if fmt.Sprintf("%x", doubleHash(nil)) != "5df6e0e2761359d30a8275058e299fcc0381534545f55cf43e41983f5d4c9456" {
10		t.Fatal("wrong hash")
11	}
12}

Exercise

Compare the digest of empty bytes with the digest of the ASCII string 00.

Reference

https://pkg.go.dev/crypto/sha256

Series navigation

https://thamer.org/learn/bitcoin-development-libraries-go

Previous lesson:
https://thamer.org/learn/bitcoin-satoshis-amounts-go

Next lesson:
https://thamer.org/learn/bitcoin-hash160-go

Questions & Answers

Explore all questions →

Latest from the Lab

THAMER illustrated developer portrait and signature

ABOUT THE DEVELOPER

Independent Software Developer specializing in Bitcoin Script, Golang, and AI Agents.

Software engineering · Independent research · Open-source projects