【发布时间】:2012-08-01 13:43:40
【问题描述】:
我们正在尝试使用 Python 匹配已通过 Oracle 的 MD5 哈希算法的哈希。根据他们的forums,所有内容在散列之前都以 AL21UTF8 编码:
-- Prior to encryption, hashing or keyed hashing, CLOB datatype is
-- converted to AL32UTF8. This allows cryptographic data to be
-- transferred and understood between databases with different
-- character sets, across character set changes and between
-- separate processes (for example, Java programs).
--
起初我认为 UTF-8 已经足够好,但如果我这样做了,我的哈希值仍然不匹配。所以经过额外的挖掘,我发现了这个article,它来自Oracle's Database Companion CD installation Guide:
AL32UTF8 是适用于 XMLType 数据的 Oracle 数据库字符集。相当于 IANA 注册的标准 UTF-8 编码,支持所有有效的 XML 字符。
不要将 Oracle 数据库数据库字符集 UTF8(无连字符)与数据库字符集 AL32UTF8 或字符编码 UTF-8 混淆。数据库字符集 UTF8 已被 AL32UTF8 取代。不要对 XML 数据使用 UTF8。 UTF8 仅支持 Unicode 3.1 及更早版本;它不支持所有有效的 XML 字符。 AL32UTF8 没有这个限制。
所以我不能使用 UTF-8,也无法弄清楚如何让 Python 的编解码器模块区分 utf-8 和 utf8。如果我尝试 AL32UTF8,它会引发错误。有没有其他人在 Python 中用 AL32UTF8 编码过?
我的编解码器代码如下所示:
import codecs
sourceFmt = "ascii"
targetFmt = "utf8"
utfFile = "kesa_utf8.dat"
with codecs.open(old, "rU", sourceFmt) as sourceFile:
with codecs.open(utfFile, "w", targetFmt) as targetFile:
targetFile.write(sourceFile.read())
文件本身如下所示:
WC000|IC |KESA |KESA | | | |2012-07-31-15.12.36 |0090| | |\c\n
WC001|100534 |W.47212-0100534 |2012-07-31-15.12.36 | 00000000001270.00|USD|\c\n
WC002|100534 |W.47212-0100534 |Sally |H |Klass |1235 14th St. W. || |Palma Sola ||FL |USA |34209 | | | | | | | | |9412587545 | | |O | | ||20800426|645858741 |SSN | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | || | | | || | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |KESAPC | | | | | |N| | | || | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |\c\n
WC999|1000000000|1000000000|4000000000|
哈希应该是 86D993FA7121E3B9EE1657A23345FE21
无论如何,我使用 hashlib 对其进行哈希处理:
import hashlib
with open(path) as f:
data = f.read()
mdhash = hashlib.md5(data)
mdhash = mdhash.hexdigest()
print mdhash
结果为 8421877dd9cdf7235eec47765821998c
【问题讨论】:
-
能否使用Oracle中的
convert函数将AL32UTF8字符编码为UTF-8? -
我猜你误解了这篇文章。 UTF8 有点奇怪,但与您的问题无关。 Oracle 的 AL32UTF8 在其他地方就是所谓的 UTF-8。请出示您的代码并展示示例。
-
ASCII 和 UTF-8 中的相同字符之间没有逐字节的区别。
-
@Ben - 我们自己没有预言机。它来自使用 oracle 的客户。 Codo - 我添加了代码和示例