Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

sha1

Calculates the SHA-1 hash of a string.

Syntax

sha1(string $string, bool $binary = false): string

Parameters

ParameterTypeDescription
$stringstringThe input string.
$binaryboolOptional. `true` returns 20 raw bytes instead of text.

Return value

stringA hash of 40 hexadecimal characters.

Examples

The hash of a string:

<?php
 
echo sha1('hello');

Output:

aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d

Notes

SHA-1 is unsuitable for passwords and signatures. Use password_hash() for passwords and hash('sha256', …) for integrity checks.

See also

  • md5 — Returns the MD5 hash of a string.
  • hash — Returns the hash of a string using a chosen algorithm.
  • password_hash — Creates a password hash for storing in a database.

In the tutorial

  • Security — The basics of PHP site security — XSS and escaping, SQL injection, password hashing, CSRF tokens and validating input.

Thanks for reading!

Donate to the site — we work hard for you.