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 bytes:
- Append
0x80: A single1bit followed by seven0bits. - Append zero bytes: Pad with
0x00bytes until the message length (leaving 8 bytes at the block tail). - Append 64-bit length: The final 8 bytes (16 hex chars) encode the original length in bits () as a big-endian integer.
- 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
{
"op": "pad",
"hex": "616263"
}
Output Format
{
"hex": "61626380000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000018"
}
Step-by-Step Implementation Guide
- Calculate lengths:
origByteLen = hex.length / 2,origBitLen = BigInt(origByteLen * 8). - Append
0x80: Append"80"to hex string. - Calculate zero padding: Pad with
"00"until byte length . - Append 64-bit big-endian bit length:
origBitLen.toString(16).padStart(16, "0"). - Emit output: Print JSON object.
Acceptance Criteria
- ✅ Appends
0x80sentinel. - ✅ 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.