Anonymizace řetězce

Z PostgreSQL
Skočit na navigaci Skočit na vyhledávání
CREATE OR REPLACE FUNCTION anonymize(text)
RETURNS text AS $$
  SELECT string_agg(CASE WHEN ascii(c) BETWEEN ascii('a') AND ascii('z') THEN substring('abcdefghijklmnopqrstuvwxyz' FROM (random()*25)::int+1 for 1) 
                         WHEN ascii(c) BETWEEN ascii('A') AND ascii('Z') THEN substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ' FROM (random()*25)::int+1 for 1) 
                         WHEN c BETWEEN '0' AND '1' THEN ((c::int + (random()* 10)::int) % 10)::text 
                         ELSE c END,'') 
     FROM regexp_split_to_table($1,'') c;
$$ LANGUAGE sql;

postgres=> select anonymize('Jmenuji se Pavel Stehule a bydlim ve Skalici 12 (qjqj) *** jkDJ ***');
                              anonymize                              
---------------------------------------------------------------------
 Jfwmuco gh Owmlx Ttsrlcb r vwtucl em Rnsoamq 72 (sxdp) *** koNF ***
(1 row)