Bỏ qua tới nội dung chính

Exness hoàn 100% hoa hồng IB

Không khoản nào bị trừ trước khi về tài khoản bạn

EA và indicator miễn phí

XM hoàn 90%, nạp tối thiểu 5 USD

How does hashing work, and what is it used for?

A hash function pads its input, cuts it into blocks of equal size and mixes each block into a running state over many rounds of bit operations. The final state is the hash. Crypto uses this to check files, store passwords, chain blocks together, summarise transactions in a Merkle root, build addresses and set the puzzle that miners solve.

Nội dung này hiện chỉ có bản tiếng Anh.

5 phút đọcAutoFXRebate

A hash function accepts a 5-byte word or a 4-gigabyte video and returns 64 characters for both. Somehow it squeezes any amount of data into the same small space, the same way every time.

This lesson opens up SHA-256, the function Bitcoin relies on, then follows hashing into the jobs it does. If the word hash is new to you, start with what hashing is.

What happens inside SHA-256

SHA-256 works through your data in fixed steps:

  1. It turns the input into bits. The word hello is 5 bytes, which is 40 bits.
  2. It pads the input: a single 1 bit, then 0 bits, then the original length as a 64-bit number, until the total is a multiple of 512 bits.
  3. It cuts the padded input into blocks of 512 bits each.
  4. It starts from a fixed 256-bit state, the same for everyone, held as eight 32-bit numbers.
  5. It mixes the first block into that state over 64 rounds, then the second block into the result, and so on.
  6. After the last block, the state is the hash.

Each round rotates bits, combines numbers with an operation called XOR, and adds numbers that wrap around when they grow too large. By the last round, every bit of the state depends on every bit of the block.

SHA-256 cuts the padded input into 512-bit blocks and mixes each one into a running state over 64 rounds; the final state is the 256-bit hash.
Every block passes through 64 rounds, and the state carries forward to the next block.

The running state is why a change anywhere matters. Edit the first byte of a large file and the state after block 1 changes, which changes the state after block 2, and so on to the end.

The mixing is thorough. The inputs hello and hello. differ by one full stop, yet their hashes differ in 118 of 256 bits, close to the half you would expect by pure chance.

No one has found a way to work backwards from a hash to an input that produces it. That is what people mean when they call SHA-256 one-way.

Worked example: how many blocks a file needs

Take a file of exactly 1,000 bytes, a round size chosen for illustration.

  1. 1,000 bytes × 8 = 8,000 bits.
  2. Padding adds at least 65 bits: the single 1 bit plus the 64-bit length. That makes at least 8,065 bits.
  3. The next multiple of 512 at or above 8,065 is 8,192, because 512 × 16 = 8,192.
  4. So the file becomes 16 blocks, and SHA-256 runs 16 × 64 = 1,024 rounds.

A short word like hello needs 40 + 65 = 105 bits, which fits inside one 512-bit block. It still goes through all 64 rounds.

Checking files and storing passwords

The simplest use is checking that data has not changed. Many software makers publish the SHA-256 hash of each download. You hash the file you received and compare, and if even one byte was altered, the hashes will not match.

This matters most for crypto wallet software. A tampered wallet app could send your keys to an attacker. A hash check catches a modified download, as long as you take the published hash from the maker's own site.

Passwords are the second everyday use. A well-built site stores a hash of your password, never the password itself. It also adds a random value called a salt, so two people with the same password end up with different hashes.

Password systems choose slow hash functions on purpose, such as bcrypt, scrypt or Argon2. A login that takes a fraction of a second is fine for you, but it makes millions of guesses slow and costly for an attacker.

Chaining blocks together

A blockchain is a list of blocks in which each block holds the hash of the block before it. That stored value is often called the previous block hash.

Say someone edits a transaction in block 100. The hash of block 100 changes from A to X. Block 101 still stores A, so the link breaks, and every later block would have to be rebuilt to hide the edit.

Three blocks linked by stored hashes; editing block 100 changes its hash from A to X, so block 101's stored value A no longer matches.
Change one old block and its link to the next block breaks.

Bitcoin also hashes the transactions inside each block into a tree. Pairs of transaction hashes are hashed together, then pairs of those results, until one hash is left. That final hash is the Merkle root.

The root is stored in the block header. A light wallet can use it to confirm that one transaction is in a block without downloading the rest. With 4 transactions, proving Tx 3 takes just 2 hashes, H4 and H12.

A Merkle tree hashes four transactions in pairs up to one root; proving Tx 3 needs only H4 and H12.
A few hashes along one branch prove a transaction sits under the root.

The proof grows slowly. With 4,096 transactions, an illustrative block size, it needs only 12 hashes, because 2 to the power of 12 is 4,096.

Mining and addresses

Bitcoin mining is a guessing game built on hashing. Miners change a number in the block header, called the nonce, and hash the header again and again until the result falls below a target.

Written in hexadecimal, a hash below the target starts with a run of zeros. Each leading zero is a 1 in 16 chance, so three zeros take 16 × 16 × 16 = 4,096 tries on average.

You can try a tiny version. Hash the text block 1, nonce 0 with SHA-256, then block 1, nonce 1, and so on, until a hash starts with 000.

Here nonce 911 is the first to work, giving a hash that starts with 000e8eef. That took 912 tries, well under the average, so luck plays a large part. Real Bitcoin targets demand far more zeros.

Many Bitcoin addresses come from hashing too. For types starting with 1 or bc1q, a wallet hashes your public key with SHA-256, then with RIPEMD-160, and encodes the result. Taproot addresses (bc1p) hold a form of the key itself.

Transaction IDs are a double SHA-256 hash of the transaction data. That ID is how you look up a Bitcoin transaction on a block explorer.

Câu hỏi thường gặp

Why does Bitcoin hash some data twice?

Block hashes and transaction IDs use SHA-256 run twice in a row. The usual explanation is that hashing twice blocks a length extension attack, a known weakness of SHA-256 run once.

Can quantum computers break SHA-256?

The best known quantum method only cuts the guessing effort to roughly its square root. For a 256-bit hash that still leaves about 2 to the power of 128 guesses, so quantum risk is usually discussed for signatures instead.

What is a Merkle root?

It is a single hash that summarises every transaction in a block. Transaction hashes are combined in pairs, level by level, until one hash remains, and that hash goes into the block header.

Is a checksum the same as a hash?

Not quite. A simple checksum only catches accidental errors, such as a corrupted download. A cryptographic hash like SHA-256 also resists deliberate tampering, because no one can craft a different file with the same hash.

Học tiếp

Chương 3What is hashing?Hashing feeds data into a hash function, which returns a short code of fixed length called a hash. The same input always gives the same hash, a tiny change gives a completely different one, and there is no practical way to work back from the hash to the input. Bitcoin uses SHA-256 hashes to link blocks and name transactions.Chương 3What is encryption?Encryption turns readable data into scrambled text using a key, and the right key turns it back. Symmetric encryption uses one shared key, while public key encryption uses a matched pair. Unlike hashing, encryption can be reversed. Bitcoin's ledger is not encrypted at all: encryption protects your connection to an exchange and the wallet files that hold your keys.Chương 3Public and private keys explained.A private key is a large random number that only you should know, and it signs your transactions. A public key is calculated from the private key and can be shared freely, so others can check your signatures and send you coins. The calculation only runs one way, so a public key never reveals the private key behind it.Chương 3How do digital signatures work?A digital signature is a number calculated from a message and a private key. Anyone with the matching public key can check it. If the check passes, the message came from whoever holds the private key and has not changed by a single bit. Bitcoin nodes run this check on every transaction before they accept it.Chương 3How does a Bitcoin transaction work?A Bitcoin transaction spends earlier unspent outputs as its inputs, proves you may spend them with a signature, and creates new outputs: one for the person you pay and usually one returning change to you. Whatever the inputs hold beyond the outputs is the fee. Nodes check the transaction, and a miner adds it to a block.
Góp ý

Tài khoản của bạn đang ở sàn nào?

Mỗi sàn một đường chuyển khác nhau, nên chúng tôi đưa bạn tới đúng phần của sàn đó.