我有一个文件的MD5十六进制摘要的字符串表示形式,我想将其转换为base64,以便在上传时使用Content-MD5 HTTP标头。是否有比以下更清晰或更有效的机制?
def hex_to_base64_digest(hexdigest)
[[hexdigest].pack("H*")].pack("m").strip
end
hex_digest = "65a8e27d8879283831b664bd8b7f0ad4"
expected_base64_digest = "ZajifYh5KDgxtmS9i38K1A=="
raise "Does not match" unless hex_to_base64_digest(hex_digest) === expected_base64_digest
答案 0 :(得分:22)
对我来说似乎非常清晰和有效。您可以通过为'm'包格式(if count is 0, no line feed are added, see RFC 4648)
指定0计数来将调用保存到条带def hex_to_base64_digest(hexdigest)
[[hexdigest].pack("H*")].pack("m0")
end