digest
PG 8.1+ (pgcrypto extension required: CREATE EXTENSION pgcrypto;)→ byteaComputes a binary hash of data using the specified algorithm. Requires the pgcrypto extension. Supported: md5, sha1, sha224, sha256, sha384, sha512.
Signatures
digest ( data text, type text ) → byteadigest ( data bytea, type text ) → byteaParameters
| Parameter | Type | Description |
|---|---|---|
| data | text or bytea | Data to hash |
| type | text | Hash algorithm: 'md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512' |
Examples
SELECT encode(digest('hello', 'sha256'), 'hex');2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824SELECT encode(digest('hello', 'md5'), 'hex');5d41402abc4b2a76b9719d911017c592SELECT encode(digest(file_content, 'sha256'), 'hex') AS integrity_hash FROM documents;Per-row SHA-256 integrity hashSELECT encode(digest(encode(payload, 'escape'), 'sha512'), 'hex') AS fingerprint FROM audit_log;SHA-512 fingerprint for audit recordsMD5 and SHA1 are cryptographically broken. Use sha256 or sha512 for any security-sensitive application. For password storage, use `crypt()` with a bcrypt or scrypt algorithm instead.
CREATE EXTENSION IF NOT EXISTS pgcrypto;
SELECT encode(digest(file_content, 'sha256'), 'hex') AS content_hash FROM documents;SHA-256 content hash for integrity verification