crypt

crypt(#[\SensitiveParameter] string $string, string $salt): string

The low-level crypt(3) one-way hash: the salt prefix selects the algorithm (DES/MD5/bcrypt/SHA-256/512) and the hash string is self-describing; demonstrates deterministic output with a fixed salt plus prefix identification - best practice is the password_hash wrapper.

C C23

C23
#include <stdio.h>
#include <string.h>

/* identify the algorithm from the self-describing prefix */
static const char *crypt_algo(const char *h) {
    if (strncmp(h, "$2a$", 4) == 0 || strncmp(h, "$2b$", 4) == 0 ||
        strncmp(h, "$2x$", 4) == 0 || strncmp(h, "$2y$", 4) == 0) return "bcrypt";
    if (strncmp(h, "$1$", 3) == 0) return "md5crypt";
    if (strncmp(h, "$5$", 3) == 0) return "sha256crypt";
    if (strncmp(h, "$6$", 3) == 0) return "sha512crypt";
    return "des"; /* classic crypt: the first two chars are the salt */
}

int main(void) {
    printf("%s\n", crypt_algo("$2y$10$N9qo8uLOickgx2ZMRZoMyeIjZAgffl4pBS4XqVv2"));
    printf("%s\n", crypt_algo("$1$salt1234$GexpiwjB8qThXlxv/yIG6."));
    printf("%s\n", crypt_algo("$6$rounds=5000$saltstring$CiGe1ZH0zTY"));
    printf("%s\n", crypt_algo("abNANd1rDfiNc"));
    return 0;
}
Output
bcrypt
md5crypt
sha512crypt
des

C++ C++26

C++26
#include <iostream>
#include <string>

static std::string crypt_algo(const std::string& h) {
    if (h.rfind("$2", 0) == 0) return "bcrypt";       /* $2a$/$2b$/$2x$/$2y$ */
    if (h.rfind("$1$", 0) == 0) return "md5crypt";
    if (h.rfind("$5$", 0) == 0) return "sha256crypt";
    if (h.rfind("$6$", 0) == 0) return "sha512crypt";
    return "des"; /* classic crypt: the first two chars are the salt */
}

int main() {
    std::cout << crypt_algo("$2y$10$N9qo8uLOickgx2ZMRZoMyeIjZAgffl4pBS4XqVv2") << "\n";
    std::cout << crypt_algo("$1$salt1234$GexpiwjB8qThXlxv/yIG6.") << "\n";
    std::cout << crypt_algo("$6$rounds=5000$saltstring$CiGe1ZH0zTY") << "\n";
    std::cout << crypt_algo("abNANd1rDfiNc") << "\n";
    return 0;
}
Output
bcrypt
md5crypt
sha512crypt
des

C# v11.0.100

v11.0.100
// .NET has no crypt counterpart: the prefix table is hand-written
string CryptAlgo(string h) => h switch
{
    { } when h.StartsWith("$2a$") || h.StartsWith("$2b$") || h.StartsWith("$2y$") => "bcrypt",
    { } when h.StartsWith("$1$") => "md5crypt",
    { } when h.StartsWith("$5$") => "sha256crypt",
    { } when h.StartsWith("$6$") => "sha512crypt",
    _ => "des", // classic crypt: the first two chars are the salt
};
Console.WriteLine(CryptAlgo("$2y$10$N9qo8uLOickgx2ZMRZoMyeIjZAgffl4pBS4XqVv2"));
Console.WriteLine(CryptAlgo("$1$salt1234$GexpiwjB8qThXlxv/yIG6."));
Console.WriteLine(CryptAlgo("$6$rounds=5000$saltstring$CiGe1ZH0zTY"));
Console.WriteLine(CryptAlgo("abNANd1rDfiNc"));
Output
bcrypt
md5crypt
sha512crypt
des

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"strings"
)

// other ecosystems re-implement the prefix table by hand
func cryptAlgo(h string) string {
	switch {
	case strings.HasPrefix(h, "$2a$"), strings.HasPrefix(h, "$2b$"), strings.HasPrefix(h, "$2y$"):
		return "bcrypt"
	case strings.HasPrefix(h, "$1$"):
		return "md5crypt"
	case strings.HasPrefix(h, "$5$"):
		return "sha256crypt"
	case strings.HasPrefix(h, "$6$"):
		return "sha512crypt"
	default:
		return "des" // classic crypt: the first two chars are the salt
	}
}

func main() {
	fmt.Println(cryptAlgo("$2y$10$N9qo8uLOickgx2ZMRZoMyeIjZAgffl4pBS4XqVv2"))
	fmt.Println(cryptAlgo("$1$salt1234$GexpiwjB8qThXlxv/yIG6."))
	fmt.Println(cryptAlgo("$6$rounds=5000$saltstring$CiGe1ZH0zTY"))
	fmt.Println(cryptAlgo("abNANd1rDfiNc"))
}
Output
bcrypt
md5crypt
sha512crypt
des

Java v27.0.1

v27.0.1
public class Main {
    static String cryptAlgo(String h) {
        if (h.startsWith("$2a$") || h.startsWith("$2b$") || h.startsWith("$2y$")) return "bcrypt";
        if (h.startsWith("$1$")) return "md5crypt";
        if (h.startsWith("$5$")) return "sha256crypt";
        if (h.startsWith("$6$")) return "sha512crypt";
        return "des"; // classic crypt: the first two chars are the salt
    }

    public static void main(String[] args) {
        System.out.println(cryptAlgo("$2y$10$N9qo8uLOickgx2ZMRZoMyeIjZAgffl4pBS4XqVv2"));
        System.out.println(cryptAlgo("$1$salt1234$GexpiwjB8qThXlxv/yIG6."));
        System.out.println(cryptAlgo("$6$rounds=5000$saltstring$CiGe1ZH0zTY"));
        System.out.println(cryptAlgo("abNANd1rDfiNc"));
    }
}
Output
bcrypt
md5crypt
sha512crypt
des

JavaScript v26.13.1

v26.13.1
// A stored hash is self-describing: the salt prefix names the algorithm
function cryptAlgo(h) {
  if (h.startsWith("$2a$") || h.startsWith("$2b$") || h.startsWith("$2y$")) return "bcrypt";
  if (h.startsWith("$1$")) return "md5crypt";
  if (h.startsWith("$5$")) return "sha256crypt";
  if (h.startsWith("$6$")) return "sha512crypt";
  return "des"; // classic crypt: the first two chars are the salt
}
console.log(cryptAlgo("$2y$10$N9qo8uLOickgx2ZMRZoMyeIjZAgffl4pBS4XqVv2"));
console.log(cryptAlgo("$1$salt1234$GexpiwjB8qThXlxv/yIG6."));
console.log(cryptAlgo("$6$rounds=5000$saltstring$CiGe1ZH0zTY"));
console.log(cryptAlgo("abNANd1rDfiNc"));
Output
bcrypt
md5crypt
sha512crypt
des

PHP v8.6.0

v8.6.0
// A stored hash is self-describing: the salt prefix names the algorithm
function crypt_algo(string $hash): string {
    return match (true) {
        str_starts_with($hash, '$2y$'), str_starts_with($hash, '$2a$'),
        str_starts_with($hash, '$2b$') => 'bcrypt',
        str_starts_with($hash, '$1$') => 'md5crypt',
        str_starts_with($hash, '$5$') => 'sha256crypt',
        str_starts_with($hash, '$6$') => 'sha512crypt',
        default => 'des', // classic crypt: the first two chars are the salt
    };
}
echo crypt_algo('$2y$10$N9qo8uLOickgx2ZMRZoMyeIjZAgffl4pBS4XqVv2'), "\n";
echo crypt_algo('$1$salt1234$GexpiwjB8qThXlxv/yIG6.'), "\n";
echo crypt_algo('$6$rounds=5000$saltstring$CiGe1ZH0zTY'), "\n";
echo crypt_algo('abNANd1rDfiNc'), "\n";
// and the live function behind it: deterministic once the salt is fixed
echo crypt('secret', 'ab'), "\n";
Output
bcrypt
md5crypt
sha512crypt
des
abNANd1rDfiNc

Python v3.15.2

v3.15.2
# The crypt module was removed in Python 3.13: the prefix table is hand-written
def crypt_algo(h):
    if h.startswith(("$2a$", "$2b$", "$2y$")):
        return "bcrypt"
    if h.startswith("$1$"):
        return "md5crypt"
    if h.startswith("$5$"):
        return "sha256crypt"
    if h.startswith("$6$"):
        return "sha512crypt"
    return "des"  # classic crypt: the first two chars are the salt

print(crypt_algo("$2y$10$N9qo8uLOickgx2ZMRZoMyeIjZAgffl4pBS4XqVv2"))
print(crypt_algo("$1$salt1234$GexpiwjB8qThXlxv/yIG6."))
print(crypt_algo("$6$rounds=5000$saltstring$CiGe1ZH0zTY"))
print(crypt_algo("abNANd1rDfiNc"))
Output
bcrypt
md5crypt
sha512crypt
des

Rust v1.99.1

v1.99.1
fn crypt_algo(h: &str) -> &'static str {
    if h.starts_with("$2a$") || h.starts_with("$2b$") || h.starts_with("$2y$") {
        "bcrypt"
    } else if h.starts_with("$1$") {
        "md5crypt"
    } else if h.starts_with("$5$") {
        "sha256crypt"
    } else if h.starts_with("$6$") {
        "sha512crypt"
    } else {
        "des" // classic crypt: the first two chars are the salt
    }
}

fn main() {
    println!("{}", crypt_algo("$2y$10$N9qo8uLOickgx2ZMRZoMyeIjZAgffl4pBS4XqVv2"));
    println!("{}", crypt_algo("$1$salt1234$GexpiwjB8qThXlxv/yIG6."));
    println!("{}", crypt_algo("$6$rounds=5000$saltstring$CiGe1ZH0zTY"));
    println!("{}", crypt_algo("abNANd1rDfiNc"));
}
Output
bcrypt
md5crypt
sha512crypt
des

TypeScript v26.13.1

v26.13.1
// Same runtime as JS: startsWith prefix identification
function cryptAlgo(h: string): string {
  if (h.startsWith("$2a$") || h.startsWith("$2b$") || h.startsWith("$2y$")) return "bcrypt";
  if (h.startsWith("$1$")) return "md5crypt";
  if (h.startsWith("$5$")) return "sha256crypt";
  if (h.startsWith("$6$")) return "sha512crypt";
  return "des"; // classic crypt: the first two chars are the salt
}
console.log(cryptAlgo("$2y$10$N9qo8uLOickgx2ZMRZoMyeIjZAgffl4pBS4XqVv2"));
console.log(cryptAlgo("$1$salt1234$GexpiwjB8qThXlxv/yIG6."));
console.log(cryptAlgo("$6$rounds=5000$saltstring$CiGe1ZH0zTY"));
console.log(cryptAlgo("abNANd1rDfiNc"));
Output
bcrypt
md5crypt
sha512crypt
des

Language differences

C: crypt(3) is the POSIX primitive (glibc ships it in libcrypt/libxcrypt), and PHP's crypt() is a thin layer over it; calling it from C needs -lcrypt and capabilities vary with the system libxcrypt version, so the example only demonstrates prefix identification (for real verification use crypt_r plus a timing-safe compare, or leave it to a library).

C++: same prefix-table idea as C; std::string::rfind(prefix, 0) == 0 checks a prefix.

C#: .NET has no crypt counterpart — the $2y$ family is implemented by BCrypt.Net / ASP.NET.Identity, with verification and needs-rehash at the framework layer; the example is the same hand-written prefix table.

Go: no crypt in stdlib; $2y$ verification goes through golang.org/x/crypto/bcrypt (its GenerateFromPassword output interoperates with PHP password_hash); the other crypt prefixes have no official implementation.

Java: the JDK has neither crypt nor bcrypt (PBKDF2 only); the prefix table is trivial to hand-write, while verification is delegated to jBCrypt/Spring Security.

JavaScript: no stdlib counterpart; npm packages like bcryptjs handle $2y$, and the rest of the crypt prefix family is essentially unimplemented.

PHP: crypt() is the oldest one-way hashing entry point — the salt prefix selects the algorithm: a bare 2-char salt is classic DES, $1$ is MD5-crypt, $2a$/$2x$/$2y$ is bcrypt, $5$/$6$ are SHA-256/512-crypt. password_hash() is essentially its safe wrapper, and password_verify() accepts crypt() output. Since 8.0 the salt is mandatory (omitting it yields a weak hash and raises E_NOTICE). The manual warns explicitly: CRYPT_STD_DES/EXT_DES/MD5 are too fast for password storage (md5crypt was EOL'd for this, CVE-2012-3287), and SHA-256/512-crypt runtime grows with the square of the input length (CVE-2016-20013) — always use password_hash() for new passwords; bcrypt truncates at 72 bytes. Compare outputs with the timing-safe hash_equals().

Python: the crypt module was removed in 3.13 (following OpenBSD's deprecation of crypt(3)); passlib covers all the prefix families.

Rust: no crypt in stdlib; the bcrypt crate covers the $2y$ family, and the prefix table is hand-written.

TypeScript: same runtime as JS.