簡體   English   中英

從Ruby中的字符串獲取UTF-8字節

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM