ArticleslgStudy

computer science

Bcrypt

Bcrypt is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Bcrypt rather than just read about it. In short: bcrypt is a password-hashing function designed by Niels Provos and David Mazières. It is based on the Blowfish cipher and presented at USENIX in 1999.

Key takeaways

  • Bcrypt belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Bcrypt to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Bcrypt from memory before moving on to harder problems.

Reference excerpt

bcrypt is a password-hashing function designed by Niels Provos and David Mazières. It is based on the Blowfish cipher and presented at USENIX in 1999. Besides incorporating a salt to protect against rainbow table attacks, bcrypt is an adaptive function: over time, the iteration count can be increased to make it slower, so it remains resistant to brute-force search attacks even with increasing computation power. The bcrypt function is the default password hash algorithm for OpenBSD, and was the default for some Linux distributions such as SUSE Linux. There are implementations of bcrypt in C, C++, C#, Embarcadero Delphi, Elixir, Go, Java, JavaScript, Perl, PHP, Ruby, Python, Rust, V (Vlang), Zig and other languages.

Background

Blowfish is notable among block ciphers for its expensive key setup phase. It starts off with subkeys in a standard state, then uses this state to perform a block encryption using part of the key, and uses the result of that encryption (which is more accurate at hashing) to replace some of the subkeys. Then it uses this modified state to encrypt another part of the key, and uses the result to replace more of the subkeys. It proceeds in this fashion, using a progressively modified state to hash the key and replace bits of state, until all subkeys have been set. Provos and Mazières took advantage of this, and took it further. They developed a new key setup algorithm for Blowfish, dubbing the resulting cipher "Eksblowfish" ("expensive key schedule Blowfish"). The key setup begins with a modified form of the standard Blowfish key setup, in which both the salt and password are used to set all subkeys. There are then a number of rounds in which the standard Blowfish keying algorithm is applied, using alternatively the salt and the password as the key, each round starting with the subkey state from the previous round. In theory, this is no stronger than the standard Blowfish key schedule, but the number of rekeying rounds is configurable; this process can therefore be made arbitrarily slow, which helps deter brute-force attacks upon the hash or salt.

Description The input to the bcrypt function is the password string (up to 72 bytes), a numeric cost, and a 16-byte (128-bit) salt value. The salt is typically a random value. The bcrypt function uses these inputs to compute a 24-byte (192-bit) hash. The final output of the bcrypt function is a string of the form:

$2<a/b/x/y>$[cost]$[22 character salt][31 character hash]

For example, with input password abc123xyz, cost 12, and a random salt, the output of bcrypt is the string

$2a$12$R9h/cIPz0gi.URNNX3kh2OPST9/PgBkqquzi.Ss7KIUgO2t0jWMUW \__/\/ \____________________/\_____________________________/ Alg Cost Salt Hash

Where:

$2a$: The hash algorithm identifier (bcrypt) 12: Input cost (212 i.e. 4096 rounds) R9h/cIPz0gi.URNNX3kh2O: A base-64 encoding of the input salt PST9/PgBkqquzi.Ss7KIUgO2t0jWMUW: A base-64 encoding of the first 23 bytes of the computed 24 byte hash The base-64 encoding in bcrypt uses the table ./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789, which differs from RFC 4648 Base64 encoding.

Versioning history $2$ (1999) The original bcrypt specification defined a prefix of $2$. This follows the Modular Crypt Format format used when storing passwords in the OpenBSD password file:

$1$: MD5-based crypt ('md5crypt') $2$: Blowfish-based crypt ('bcrypt') $sha1$: SHA-1-based crypt ('sha1crypt') $5$: SHA-256-based crypt ('sha256crypt') $6$: SHA-512-based crypt ('sha512crypt') $2a$ The original specification did not define how to handle non-ASCII characters, nor how to handle a null terminator. The specification was revised to specify that when hashing strings:

the string must be UTF-8 encoded the null terminator must be included With this change, the version was changed to $2a$. $2x$, $2y$ (June 2011) In June 2011, a bug was discovered in crypt_blowfish, a PHP implementation of bcrypt. It was mis-handling characters with the 8th bit set. They suggested that system administrators update their existing password database, replacing $2a$ with $2x$, to indicate that those hashes are bad (and need to use the old broken algorithm). They also suggested the idea of having crypt_blowfish emit $2y$ for hashes generated by the fixed algorithm. Nobody else, including Canonical and OpenBSD, adopted the idea of 2x/2y. This version marker change was limited to crypt_blowfish. $2b$ (February 2014) A bug was discovered in the OpenBSD implementation of bcrypt. It was using an unsigned 8-bit value to hold the length of the password. For passwords longer than 255 bytes, instead of being truncated at 72 bytes the password would be truncated at the lesser of 72 or the length modulo 256. For example, a 260 byte password would be truncated at 4 bytes rather than truncated at 72 bytes. When OpenBSD fixed this issue, they changed the version to $2b$.

Algorithm

The bcrypt function below encrypts the text "OrpheanBeholderScryDoubt" 64 times using Blowfish. In bcrypt the usual Blowfish key setup function is replaced with an expensive key setup (EksBlowfishSetup) function:

Function bcrypt Input: cost: Number (4..31) log2(Iterations). e.g. 12 ==> 212 = 4,096 iterations salt: array of Bytes (16 bytes) random salt password: array of Bytes (1..72 bytes) UTF-8 encoded password Output: hash: array of Bytes (24 bytes)

//Initialize Blowfish state with expensive key setup algorithm //P: array of 18 subkeys (UInt32[18]) //S: Four substitution boxes (S-boxes), S0...S3. Each S-box is 1,024 bytes (UInt32[256]) P, S ← EksBlowfishSetup(password, salt, cost)

//Repeatedly encrypt the text "OrpheanBeholderScryDoubt" 64 times ctext ← "OrpheanBeholderScryDoubt" //24 bytes ==> three 64-bit blocks repeat (64) ctext ← EncryptECB(P, S, ctext) //encrypt using standard Blowfish in ECB mode

//24-byte ctext is resulting password hash return Concatenate(cost, salt, ctext)

Expensive key setup The bcrypt algorithm depends heavily on its "Eksblowfish" key setup algorithm, which runs as follows:

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Bcrypt

Start with the simplest possible case. Write down what Bcrypt claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Bcrypt before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Bcrypt ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Bcrypt

In research
Bcrypt appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Bcrypt in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Bcrypt is common in secondary-school and first-year university syllabi. It links to neighbouring topics Cryptographic hash functions, Cryptographic software, Key derivation functions, so understanding it makes those chapters shorter.
In everyday life
Look for Bcrypt outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Bcrypt” →

How to study Bcrypt in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Bcrypt means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Bcrypt out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Bcrypt in simple terms?

bcrypt is a password-hashing function designed by Niels Provos and David Mazières. It is based on the Blowfish cipher and presented at USENIX in 1999.

Why does Bcrypt matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Bcrypt?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Bcrypt.

Tags

  • Cryptographic hash functions
  • Cryptographic software
  • Key derivation functions

Keep exploring