-1

I'm looking to convert a hex such as

12b2b0621c79b1e57fb0ee64061ef92e8ae04a0b13173cd681addf6f2bb474f3 (longest HEX output I have)

to a passphrase with alphanumeric characters with some uppercase characters as well. Is there a way I can do this? Maybe cycle through an array of characters, not sure.

I have tried to use the

["666f6f626172"].pack('H*')

=> "foobar"

code, but I get "??" showing up as a return value. This is due to it being outside the 26 alphabet characters. I'm trying to stay within that limit

Community
  • 1
  • 1
  • Possible duplicate of [Convert string with hex ASCII codes to characters](http://stackoverflow.com/questions/22957688/convert-string-with-hex-ascii-codes-to-characters) – morxa Mar 17 '16 at 16:14
  • Welcome to Stack Overflow. Please read "[ask]" and "[mcve]". We want to see evidence of your effort. That means either where you've searched for solutions and why those didn't help, or the code you wrote toward solving the problem. As is it looks like you're asking us to write it for you which is off-topic. Please provide more details. – the Tin Man Mar 17 '16 at 17:15

1 Answers1

0

I presume the issue here is that you feel the (longest) hex string is too long to use for a password. A possible solution may be converting your hex string back to bytes, then converting that to a base that is still human-readable, but more efficient than hex (base-16).

Here's a simple base-36 implementation:

hex = '12b2b0621c79b1e57fb0ee64061ef92e8ae04a0b13173cd681addf6f2bb474f3'
base36 = hex.scan(/../).map(&:hex).map { |m| m.to_s(36) }.join
puts base36
puts hex.size
puts base36.size

results:

i4y4w2qs3d4x6d3j4w6m2s6u6x1a3u6822bjn1o5y3l4t67331750386r
64
57

We saved a few bytes here, but still not optimal.

Base64 will be slightly better:

require 'base64'

hex = '12b2b0621c79b1e57fb0ee64061ef92e8ae04a0b13173cd681addf6f2bb474f3'
base64 = Base64.encode64([hex].pack('H*'))
puts base64
puts hex.size
puts base64.size

results:

ErKwYhx5seV/sO5kBh75LorgSgsTFzzWga3fbyu0dPM=
64
45

Finally, there's an even better solution: ASCII-85. There's a gem for that:

http://ascii85.rubyforge.org/

JLB
  • 323
  • 3
  • 8