Este problema se resuelve fácilmente en 12c con la función STANDARD_HASH .
La solución en versiones anteriores es solo un poco más complicada. Cree un contenedor simple alrededor de DBMS_CRYPTO que actúe como STANDARD_HASH:
--Imitation of the 12c function with the same name.
--Remember to drop this function when you upgrade!
create or replace function standard_hash(
p_string varchar2,
p_method varchar2 default 'SHA1'
) return varchar2 is
v_method number;
v_invalid_identifier exception;
pragma exception_init(v_invalid_identifier, -904);
begin
--Intentionally case-sensitive, just like the 12c version.
if p_method = 'SHA1' then
v_method := dbms_crypto.hash_sh1;
--These algorithms are only available in 12c and above.
$IF NOT DBMS_DB_VERSION.VER_LE_11 $THEN
elsif p_method = 'SHA256' then
v_method := dbms_crypto.hash_sh256;
elsif p_method = 'SHA384' then
v_method := dbms_crypto.hash_sh384;
elsif p_method = 'SHA512' then
v_method := dbms_crypto.hash_sh512;
$END
elsif p_method = 'MD5' then
v_method := dbms_crypto.hash_md5;
else
raise v_invalid_identifier;
end if;
return rawToHex(dbms_crypto.hash(utl_raw.cast_to_raw(p_string), v_method));
end;
/
Es posible que deba iniciar sesión con SYS y otorgar a su usuario acceso a DBMS_CRYPTO para que la función funcione:
grant execute on sys.dbms_crypto to <your_schema>;
Crea un sinónimo público, concédelo a todos y funciona exactamente de la misma manera.
create public synonym standard_hash for <schema with function>.standard_hash;
grant execute on standard_hash to public;
select standard_hash('Some text', 'MD5') from dual;
9DB5682A4D778CA2CB79580BDB67083F
select standard_hash('Some text', 'md5') from dual;
ORA-00904: : invalid identifier
Aquí hay un ejemplo simple del uso de la función:
update some_table
set column1 = standard_hash(column1),
column2 = standard_hash(column2);
Pero actualizar grandes cantidades de datos puede ser lento. Puede ser más rápido crear una nueva tabla, descartar la anterior, cambiar el nombre de la nueva, etc. Y el valor hash puede ser mayor que el tamaño de la columna, puede ser necesario alter table some_table modify column1 varchar2(40 byte);
Me sorprende la cantidad de productos y herramientas que existen para hacer algo tan simple.