4

I want to convert a string to a byte array before I can sign it with a private key but I have not done it before. My code is as below could anybody give me a clue on how I can do it. Thank you.

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
1

1 Answer 1

6

Ruby has the bytes method and you can convert that to an array with 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] 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.