この問題は、関数STANDARD_HASHを使用して 12c で簡単に解決できます。
以前のバージョンのソリューションは、少しだけ複雑です。STANDARD_HASH と同じように機能する DBMS_CRYPTO の単純なラッパーを作成します。
--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;
/
関数を機能させるには、SYS でログオンし、ユーザーに DBMS_CRYPTO へのアクセス権を付与する必要がある場合があります。
grant execute on sys.dbms_crypto to <your_schema>;
パブリック シノニムを作成し、全員に付与すると、まったく同じように機能します。
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
関数を使用する簡単な例を次に示します。
update some_table
set column1 = standard_hash(column1),
column2 = standard_hash(column2);
ただし、大量のデータの更新は遅くなる可能性があります。新しいテーブルを作成し、古いテーブルを削除し、新しいテーブルの名前を変更するなどした方が速い場合があります。また、ハッシュ値が列のサイズよりも大きい場合があるため、必要になる場合があります。alter table some_table modify column1 varchar2(40 byte);
このような単純なことを行うための製品やツールがどれだけあるのかに驚かされます。