Compute the SHA-1 digest of a string as a 40-character lowercase hex string.
SHA(expr)
## Overview Computes the SHA-1 message digest of the input string and returns a 40-character lowercase hex string representing the 160-bit hash. SHA is an alias for SHA1 and produces identical output. SHA-1 is deprecated for cryptographic use. Practical collision attacks exist, so it is not suitable for signatures, certificates, or any adversarial context. It remains useful for deterministic fingerprints, deduplication, and lineage identifiers where only accidental collisions matter. ## Behavior - Returns NULL for NULL input. - Output is a 40-character hex string, lowercase. - Deterministic across calls and engines that implement RFC 3174. - Strings are hashed as UTF-8 bytes. - Output is identical to SHA1 for the same input. ## Algorithm - SHA-1 per RFC 3174. - Output width: 160 bits (20 bytes), rendered as 40 hex characters. ## Compatibility - Output matches RFC 3174 / FIPS 180-4 reference vectors across conformant implementations. - Case of hex output may vary across engines; normalize with LOWER() when comparing.
| Name | Type | Description |
|---|---|---|
expr | Specifies the string value to hash. The string is hashed as its UTF-8 byte sequence. Returns NULL for NULL input. |
-- SHA-1 of a simple string
SELECT SHA('hello') AS h; -- 'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d'
-- SHA-1 of the empty string
SELECT SHA('') AS h;
-- Length is always 40 hex characters
SELECT LENGTH(SHA('test')) AS len; -- 40
-- SHA is an alias for SHA1 and produces identical output
SELECT SHA('data') = SHA1('data') AS same; -- true
-- Realistic: fingerprint rows for lineage tracking
SELECT order_id, SHA(CONCAT_WS('|', customer_id, CAST(total AS STRING))) AS fp
FROM ecommerce.sales.orders;