Hash and encryption functions in DB2

Some columns are secrets. Some are only fingerprints. DB2 for z/OS gives you one-way HASH functions, two-way encryption (ENCRYPT_DATAKEY with AES-256 key labels, and the deprecated ENCRYPT_TDES password functions plus GETHINT), and GENERATE_UNIQUE for time-based unique tokens. This page sticks to z/OS names from the SQL Reference and maps the checklist names HASH4 / HASH8 / HASH16 and ENCRYPT_DATA / DECRYPT_DATA onto what actually exists on the mainframe.

SQL security functions
Progress0 of 0 lessons

HASH and HASH_algorithm

HASH applies a cryptographic hash algorithm to an input and returns binary bytes. Schema is SYSIBM. Named twins HASH_MD5, HASH_SHA1, HASH_SHA256, and HASH_CRC32 exist so ports and new SQL can say the algorithm in the function name.

sql
1
2
3
4
5
HASH(expression [, algorithm]) HASH_MD5(expression) HASH_SHA1(expression) HASH_SHA256(expression) HASH_CRC32(expression)
HASH algorithm argument on Db2 for z/OS
HASH second argumentAlgorithmResult
0 (default)MD5 / HASH_MD5VARBINARY(16) — 128-bit; not for new secrets
1SHA-1 / HASH_SHA1VARBINARY(20) — 160-bit; not for new secrets
2SHA-256 / HASH_SHA256VARBINARY(32) — 256-bit; preferred of this set
HASH_CRC32CRC32 checksumIntegrity / lookup checksum, not a password hash
sql
1
2
3
4
SELECT HASH(LASTNAME, 2) AS SHA256, HASH_SHA256(LASTNAME) AS SHA256_NAMED FROM DSN8C10.EMP WHERE EMPNO = '000010';

Null input yields a null hash. Hashes are for integrity checks, change detection, and (with care) fingerprints. They are not encryption. MD5 and SHA-1 are broken for collision resistance; do not use them for new security designs. SHA-256 is the least-wrong choice in this built-in set. Real password storage belongs in an application library with a slow salted hasher (bcrypt/argon2), not a single SQL HASH call.

HASH4, HASH8, HASH16

Training outlines and Db2 LUW manuals list HASH4, HASH8, and HASH16 as fast checksum lookup functions (INTEGER / BIGINT / 128-bit), with Adler, CRC32, or Jenkins algorithms. The Db2 for z/OS SQL Reference documents HASH and HASH_algorithm instead. On z/OS, use HASH_CRC32 when you want a CRC-style checksum, and HASH / HASH_SHA256 when you want a cryptographic digest. Do not paste LUW HASH4(..., 0) examples into a z/OS package without verifying the name on your function level.

LUW checklist names (not z/OS HASH syntax)
NameTypical LUW meaning
HASH4LUW: 32-bit INTEGER checksum (Adler default, or CRC32)
HASH8LUW: 64-bit BIGINT checksum (Jenkins); endian-sensitive
HASH16LUW: 128-bit binary checksum family for lookups

ENCRYPT_DATAKEY and DECRYPT_DATAKEY_type

These are the modern column-encryption functions (Db2 12 function level 505 and later). The checklist names ENCRYPT_DATA / DECRYPT_DATA refer to this family on z/OS.

sql
1
ENCRYPT_DATAKEY(expression, key-label [, 'AES256D' | 'AES256R'])
  • expression — value to encrypt (integer, decimal, character, graphic, and LOB forms as documented). Result is VARBINARY or BLOB.
  • key-label — ICSF CKDS key label. Knowing the label is useless without RACF authority to use the key. The label is stored as metadata with the ciphertext, so decrypt functions do not take the label again.
  • AES256D — deterministic IV: same cleartext encrypts to the same ciphertext (can support equality lookups; weaker against frequency analysis).
  • AES256R — random IV: same cleartext encrypts differently each time (stronger; no simple equality search on ciphertext).

Decrypt with a typed function that matches what you encrypted:

  • DECRYPT_DATAKEY_INTEGER
  • DECRYPT_DATAKEY_BIGINT
  • DECRYPT_DATAKEY_DECIMAL (precision and scale)
  • DECRYPT_DATAKEY_VARCHAR / CLOB / VARGRAPHIC / DBCLOB (CCSID as documented)
  • DECRYPT_DATAKEY_BIT

Encrypting INTEGER and decrypting with DECRYPT_DATAKEY_VARCHAR fails (SQLCODE −171). There is no CHAR decrypt form — strings come back varying-length. Ciphertext is longer than cleartext; size the VARBINARY/BLOB column accordingly.

ENCRYPT_TDES, ENCRYPT, decrypt, and GETHINT (deprecated)

ENCRYPT_TDES (synonym ENCRYPT) applies Triple DES with a password. IBM marks it deprecated: the algorithms are not considered quantum-safe. Prefer ENCRYPT_DATAKEY.

sql
1
2
3
4
5
ENCRYPT_TDES(string-expression [, password-string [, hint-string]]) DECRYPT_CHAR(encrypted, password) DECRYPT_BIT(encrypted, password) DECRYPT_DB(encrypted, password) GETHINT(encrypted-data)
  • Password can be the second argument or the ENCRYPTION PASSWORD special register (SET ENCRYPTION PASSWORD ... WITH HINT ...).
  • Encrypted columns are VARCHAR (or similar) because the result is binary-looking data plus metadata. Length grows (block padding plus 24 bytes of metadata, plus up to 32 bytes if a hint is stored).
  • GETHINT returns the hint that was stored with that encrypted value — a reminder, not the password.

Never hard-code production passwords in SQL text, host variables logged in traces, or tables. That design is exactly why key labels replaced password arguments.

GENERATE_UNIQUE and GENERATE_UNIQUE_BINARY

These functions return a value unique compared with any other execution of the same function. Empty parentheses are required. They are not deterministic. The result cannot be null.

  • GENERATE_UNIQUE() — CHAR(13) FOR BIT DATA.
  • GENERATE_UNIQUE_BINARY() — BINARY(16).

The value embeds UTC time and, in a sysplex, the member that processed the function. TIMESTAMP(unique-id) recovers that timestamp. Unlike CURRENT TIMESTAMP, each row of a multiple-row INSERT, INSERT with fullselect, or MERGE insert gets a distinct value.

sql
1
2
3
4
5
6
7
8
9
10
CREATE TABLE EMP_UPDATE ( UNIQUE_ID VARCHAR(13) FOR BIT DATA, EMPNO CHAR(6), TEXT VARCHAR(1000) ); INSERT INTO EMP_UPDATE VALUES (GENERATE_UNIQUE(), '000020', 'Update entry 1...'); SELECT TIMESTAMP(UNIQUE_ID), EMPNO, TEXT FROM EMP_UPDATE;

A BEFORE INSERT trigger can force the column:

sql
1
2
3
4
5
CREATE TRIGGER EMP_UPDATE_UNIQUE NO CASCADE BEFORE INSERT ON EMP_UPDATE REFERENCING NEW AS NEW_UPD FOR EACH ROW MODE DB2SQL SET NEW_UPD.UNIQUE_ID = GENERATE_UNIQUE();

What these functions are not

  • Not disk encryption — use DFSMS data set encryption / pervasive encryption for tablespaces and logs.
  • Not SSL/TLS for DDF — that is AT-TLS / certificate work.
  • Not row permissions or column masks — those are separate security objects.

Explain It Like I'm Five

A hash is a blender: you pour in a secret recipe and get a smoothie colour. You can check later whether a new recipe blends to the same colour, but you cannot un-blend the smoothie back into tomatoes. Encryption is a locked lunchbox: ENCRYPT_DATAKEY puts the sandwich in and tags the box with a label of which school key opens it. Only kids RACF says may use that key can open it. The old ENCRYPT_TDES lunchbox used a password written on a sticky note — IBM says stop doing that. GENERATE_UNIQUE is a ticket machine that never gives two people the same number, even when a whole crowd presses the button at once.

Exercises

  1. Write HASH_SHA256 and the equivalent HASH(expr, 2) on LASTNAME.
  2. Explain why HASH is a bad way to store user passwords compared with a salted application hasher.
  3. Contrast AES256D and AES256R for a National Insurance number column that must sometimes be searched.
  4. State what GETHINT returns and why it must not be treated as the TDES password.
  5. Write GENERATE_UNIQUE into a VARCHAR(13) FOR BIT DATA column and select TIMESTAMP of that column.

Quiz

Test Your Knowledge

1. HASH(expr, 2) on Db2 for z/OS applies which algorithm?

  • MD5
  • SHA-1
  • SHA-256
  • CRC32

2. Why is ENCRYPT_DATAKEY preferred over ENCRYPT_TDES?

  • TDES is faster so it must be worse
  • ENCRYPT_TDES uses a password (often in the application) and is deprecated / not quantum-safe; ENCRYPT_DATAKEY uses AES-256 and a RACF-protected key label
  • ENCRYPT_DATAKEY only works on DATE
  • They are identical

3. What does GETHINT return?

  • The encryption password in clear text
  • The optional password hint stored with an ENCRYPT_TDES value
  • A RACF key label
  • Always NULL

4. GENERATE_UNIQUE() returns:

  • INTEGER
  • CHAR(13) FOR BIT DATA including UTC time (and sysplex member); not null
  • A DATE only
  • XML

5. Are HASH4, HASH8, and HASH16 built-in on Db2 for z/OS the same way as on LUW?

  • Yes, identical INTEGER checksum functions in every z/OS SQL Reference
  • They are LUW checksum lookup functions; z/OS documents HASH / HASH_algorithm (MD5, SHA-1, SHA-256, CRC32) instead — do not copy LUW HASH4 examples onto z/OS without checking your function level
  • They decrypt TDES
  • They create tablespaces

Frequently Asked Questions