我想先将字符串转换为字节数组,然后才能使用私钥对其进行签名,但我以前没有这样做过。我的代码如下,任何人都可以给我一个关于如何做到这一点的线索。谢谢你。
require 'openssl'
require 'base64'
require 'digest/sha1'
@date = Date.today.strftime("%m/%d/%Y")
text_to_sign = "#{@order.phone_no}" + "#{@order.name}" + "#{@order.pay_type}" + "#{@order.pay_type}" + "1" + "MABIRA" + "81W30DI846" + "#{@date}" + "PULL" + "1" + "#{@cart.total_price}" + "#{@order.phone_no}" + ""
password = 'secret'
#converting the string to byte array
byte[] buff => new byte[1024]
text = buff.text_to_sign
private_key = OpenSSL::PKey::RSA.new(File.read('Private.key'), password)
ciphertext = private_key.private_encrypt(text)
ciphertext.encoding
signed_text = Base64.encode64(ciphertext).gsub("\n", '')
puts signed_text
signed_text
最佳答案
Ruby 有 bytes
方法,您可以使用 to_a
将其转换为数组
http://www.ruby-doc.org/core-1.9.3/ARGF.html#method-i-bytes
string = 'some string'
byte_array = string.bytes.to_a
# [115, 111, 109, 101, 32, 115, 116, 114, 105, 110, 103]
关于ruby-on-rails - 如何在 ruby on rails 中将字符串转换为字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24695440/