Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1#!/usr/bin/env python32# Copyright (c) 2023-present The Bitcoin Core developers3# Distributed under the MIT software license, see the accompanying4# file COPYING or http://www.opensource.org/licenses/mit-license.php.6"""Test-only HKDF-SHA256 implementation8It is designed for ease of understanding, not performance.10WARNING: This code is slow and trivially vulnerable to side channel attacks. Do not use for11anything but tests.12"""14import hashlib15import hmac18def hmac_sha256(key, data):19"""Compute HMAC-SHA256 from specified byte arrays key and data."""20return hmac.new(key, data, hashlib.sha256).digest()23def hkdf_sha256(length, ikm, salt, info):24"""Derive a key using HKDF-SHA256."""25if len(salt) == 0:26salt = bytes([0] * 32)27prk = hmac_sha256(salt, ikm)28t = b""29okm = b""30for i in range((length + 32 - 1) // 32):31t = hmac_sha256(prk, t + info + bytes([i + 1]))32okm += t33return okm[:length]
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.