Build SHA-256 and HMAC from scratch
Stage 1 of 7v2 · 157608d8

SHA-256 padding

Append the one bit, zeroes, and the 64-bit length to complete 512-bit blocks.

Build it in your language

The tester treats your program as a black box: any language works. These are just the most convenient ones for this project.

SHA-256 Message Padding

SHA-256 processes messages in fixed 64-byte (512-bit) blocks. The cryptographic standard (FIPS 180-4) specifies exact deterministic padding so any payload fills an integer number of blocks.

Mental Model: The Padding Rule

For an input message of LL bytes:

  1. Append 0x80: A single 1 bit followed by seven 0 bits.
  2. Append zero bytes: Pad with 0x00 bytes until the message length equiv56pmod64equiv 56 pmod{64} (leaving 8 bytes at the block tail).
  3. Append 64-bit length: The final 8 bytes (16 hex chars) encode the original length in bits (Limes8L imes 8) as a big-endian integer.
  4. Total resulting length is always a multiple of 64 bytes (128 hex chars).

Your Goal in this Stage

Read a JSON object from stdin with op: "pad" and hex data. Output the fully padded string under "hex".

Input Format

json
{
  "op": "pad",
  "hex": "616263"
}

Output Format

json
{
  "hex": "61626380000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000018"
}

Step-by-Step Implementation Guide

  1. Calculate lengths: origByteLen = hex.length / 2, origBitLen = BigInt(origByteLen * 8).
  2. Append 0x80: Append "80" to hex string.
  3. Calculate zero padding: Pad with "00" until byte length equiv56pmod64equiv 56 pmod{64}.
  4. Append 64-bit big-endian bit length: origBitLen.toString(16).padStart(16, "0").
  5. Emit output: Print JSON object.

Acceptance Criteria

  • ✅ Appends 0x80 sentinel.
  • ✅ Aligns to 56 bytes mod 64.
  • ✅ Appends 64-bit big-endian original bit length.

Starting and checking progress

Open your language starter’s README. From its root, execute the build command in shipcode.yml if present, then run. The scaffold is intentionally unfinished and initially reports missing implementation. After developing this stage, run shipcode test; preserve earlier stages’ behavior and use shipcode submit after passing.

You may use a JSON library for the input/output envelope, but not to implement the project’s algorithm. If your language has no built-in JSON support, document the dependency and update the shipcode.yml build command; verify installation in a clean directory.

Snippets using Math, .shift(), or similar notation illustrate algorithm ideas; they do not require JavaScript. Adapt them to your language. A byte is not always a character: count bytes in binary formats and specify the encoding.

Output and errors

Multiline examples are formatted for readability. Emit compact JSON without optional spaces, with properties in the order shown by the output examples, and a final newline. Keep logs off stdout. Tests compare exact text. Invalid inputs use stderr containing error, empty stdout, and exit 1. Valid inputs exit 0. Serialize integer results as JSON integers.