Oracle检查sha512加密数据

时间:2015-04-08 09:09:25

标签: oracle encryption oracle11g sha512

我需要通过Oracle查询检查sha512加密中的数据。

示例查询:

SELECT * FROM text WHERE SHA512(id) = '$id'

1 个答案:

答案 0 :(得分:1)

Oracle提供DBMS_CRYPTO包来处理加密和哈希。

您可以按照此查询中显示的方式将SHA哈希计算和比较与某个字符串常量结合使用:

select *
from text t
where 
  lower(                           -- to guarantee same character case 

    rawtohex(                      -- convert hash to string representation  
      dbms_crypto.hash(            -- hash calculation function

        utl_raw.cast_to_raw(t.id), -- need to convert a string before passing it to 
                                   -- function in parameter because otherwise 
                                   -- it casted to RAW directly with hextoraw().

        6                          -- dbms_crypto.HASH_SH512 - for Oracle 12c only 
      )
    )
  )
  = 
  lower(                           -- to guarantee same character case   
    :some_id_sha2_const_parameter  -- parameter string to compare with
  ) 

不幸的是,由于缺乏支持,您无法在Oracle 11g中处理SHA-512哈希值。但是Oracle 12c可以实现这一点。

文档:
DBMS_CRYPTO package in Oracle 11g
DBMS_CRYPTO package in Oracle 12c

您还必须考虑两件事:

  1. 确保源字符串的字符集相同,因为哈希是根据二进制数据计算的。
  2. 从性能的角度来看,最好将预先计算的哈希值存储在列中的数据库中,对其进行索引,并将外部参数与存储在列中的值进行比较。