open3 popen3 binary data
I'm trying to send binary data through popen3, however I am getting an error.
The shell command I'm issuing is:
key = File.open('path.key').read
Open3.popen3("openssl pkcs8 -inform DER -outform PEM -passin pass:#{password}") do |stdin, stdout, stderr|
stdin.print(key)
unless (err = stderr.read).empty? then raise err end
stdout.read
end
where key is a binary file. OpenSSL says the key isn't valid, and I'm scratching my head.
Now, if I issue the command by having OpenSSL read it directly from the file system instead of passing it through the standard input it works correctly
"openssl pkcs8 -in path.key -inform DER -outform PEM -passin pass:#{password}"
Is there an encoding issue I'm not aware of? Or is this simply impossible. Since I will be receiving the keyfiles through a webservice, I would rat开发者_JAVA技巧her not have to save them to disk and have openssl read them, and instead do the whole process in memory.
Thank you for your time
Try this:
Open3.popen3("openssl pkcs8 -inform DER ...") do |stdin, stdout, stderr|
stdin.reopen(File.open('path.key', 'rb'))
unless (err = stderr.read).empty? then raise err end
stdout.read
end
Instead of reading the file into a string, reopen the stdin of the process to be the file. Also, just to be on the safe side, open it in binary mode (notice the rb
mode in the File.open
call)
精彩评论