---
title: "EVP_BytesToKey"
description: "The key and IV behind openssl enc without -pbkdf2 and behind CryptoJS with a passphrase. Old and weak and still everywhere"
canonical_url: "https://hashes.agntn.dev/algorithms/evp-bytestokey"
---
# EVP_BytesToKey

> The key and IV behind openssl enc without -pbkdf2 and behind CryptoJS with a passphrase. Old and weak and still everywhere

<algorithm-facts name="evp-bytestokey">



</algorithm-facts>

Ever seen base64 that starts with `U2FsdGVkX1`? That's `Salted__`, and somebody ran `openssl enc` or `CryptoJS.AES.encrypt(message, passphrase)`. Both get the key and the IV from the passphrase with EVP_BytesToKey. CryptoJS calls it EvpKDF. Same recipe, two names.

And the recipe is short. Hash the password and the salt. Hash that block again with the password and the salt behind it. Repeat until the key and the IV fit. Set `iterations` and each block goes through the hash that many times before the next one starts.

```ts
create("evp-bytestokey").hash("password", { salt: "0102030405060708" }).digest;
// "e7b0971e52ca5cc8d0539fb3412f6316f7ba2e6ee293d9f3457b99436b51ce028d450e2ed75a84a923d4eac9fe49226b"
```

That's `openssl enc -aes-256-cbc -P -md md5 -pass pass:password -S 0102030405060708`, byte for byte. The first 32 bytes are the key and the last 16 the IV. Where's the split? `keyLength` and `ivLength` set it, and the result names both.

`digest` is `md5` by default. CryptoJS uses it, and so did OpenSSL before 1.1.0. Newer `openssl enc` takes `sha256`, and `sha1` is there too. The salt is the 8 bytes after `Salted__`, in hex. Leave it out and there's no salt, like `-nosalt`. Not a random one, because the format keeps its salt next to the ciphertext anyway. CryptoJS lets a page ask for any key size, so `keyLength` goes way past 32. A cipher without an IV takes `ivLength` 0.

Writing the cipher side yourself? Skip the result object. `evpBytesToKey` takes a hasher and bytes and gives back bytes, like `pbkdf2` does:

```ts
import { evpBytesToKey } from "@agntn/hashes/evp";
import { Md5Hasher } from "@agntn/hashes/md5";

const password = new TextEncoder().encode("password");
const salt = Uint8Array.fromHex("0102030405060708");
const derived = evpBytesToKey(() => new Md5Hasher(), password, salt, 1, 48);
// the same 48 bytes, key first and IV after it
```

Order goes iterations, then length. It takes a salt of any length, empty for none. The check for 8 bytes lives in `create()`, which reads OpenSSL's format.

Encrypt something new with it? Please don't. One MD5 per block is nothing for a GPU. It's here to open what somebody already locked. Your own keys want [scrypt](https://hashes.agntn.dev/algorithms/scrypt).


## Sitemap

See the full [sitemap](https://hashes.agntn.dev/sitemap.md) for all pages.
