[英]How to convert array of UCS-2 bytes to UTF-8 string in Ruby?
[英]Get UTF-8 bytes from a string in Ruby
我需要“對字符串的UTF-8字節執行SHA1哈希處理”。 我不確定如何獲取UTF-8字節。 這是我應該得到的示例:
original = "http://www.provider.com/article?query=mysearch&abcd&1300459467&our secret key"
#Perform the sha1 hashing on the UTF-8 bytes of this string, to get ...
expected_hashed = "99802fec87b6ef1d45bd07f3053d13 6cfcfbdf0b"
#... which is a 160 byte fingerprint.
#You need to then take the 20 byte representation of this string (make sure you're
#not just taking the hex string), and base 64 encode that.
expected_encoded = "mYAv7Ie27x1FvQfzBT0TbPz73ws="
它從original
變為expected_hashed
的問題,這給了我一個問題:我不知道如何取出UTF-8字節。 我正在像Digest::SHA1.digest(unhashed_string)
一樣進行SHA1哈希Digest::SHA1.digest(unhashed_string)
,但我也不100%確信在這種情況下是否合適。 :/我也不確定“此字符串的20字節表示形式”是什么。
require 'digest'
require 'base64'
Base64.encode64(Digest::SHA1.digest(original)[0,20])
# => "mYAv7Ie27x1FvQfzBT0TbPz73ws=\n"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.