javascript - Encoding and byte errors in Ruby using OpenSSL? -
i getting error:
incompatible encoding regexp match (utf-8 regexp ascii-8bit string)
when trying pass variable rails javascript via line:
window.nonce = '<%= j @nonce %>';
my @nonce
variable openssl rsa public key encryption created by:
@nonce = rails.rsaencusingpublic(pubkey, randomstr) def self.rsaencusingpublic(key, msg) return key.public_encrypt msg end
i tried adding force_encoding("utf-8")
end of rsaencusingpublic
function changed error to:
invalid byte sequence in utf-8
now don't want strip characters encrypted variable, same encryption function works fine everywhere else, until i'm passing javascript.
printing out @nonce
(with or without force encoding) give lot of gibberish:
#??7:a}p[?ͼg/?%ŋ??=????h9?jg? w?v?j ?}??g???t?7?i?:"???"#hm?0?l?????*r:ɦyab&v&?5mǓŌ,???[u?dt??g???tև?&~??6f???????p??<gkv?`? p?k??b???????[?yj6?=?;? ???p?j=?z?? ?[?5i??i?t?!???^?l{;??,??ma\_mg?|??]a?????"??x:??;??? ?y??\纘???# ]?m" ? ?n
@nonce.encoding
prints out utf-8
.
@nonce.inspect
pints out:
"\u0015\xc0jn\xe7\xbc\xe4\u0016gv\x84&-ˌ+Śa:4\xb1(\xc0\xeav\x91\xe8>\u001d\x92ږ\xf6\xdc\xee\x9a)\xc7&o\u001a\x90fเ\e\x9bb*\xf2\xe2\u001e\xb9v\x9e\xbb\x9auЕcu\u001e~\u0011\u0001$մ\xf8j\xed\xfe^\"\u001ec\xbd8\u0002\xba\xdc\xdfiЊ, ku\u0000\u0014\u0015\x92_w\x95\x89\xd0-ofg\xb5\xf8lc\x9bo\\0j<ƥ\xa5\u001dw(t?\xa4\xa2\u00174\xb5Š\xe3\x91s\xda\u0002i\xb3\u0003q\u000f\xf4\xdb5\x80\xd8\xe0./\x8b\x8a߳0\u0001\x91=$t\xcb\blh\xf3\u001c\xfd\xbf\x95i%=gq\u000f}\x8f_w\xfan\x90\x81\xfc\b4\x9e\xc1\xd7y\xbc\xe8\xa4cqy\xb2@s1\xd7\xc9+\xa7\xea>\xa5\xbc\xcf\xc81:tg\xfd\x88\xccs\x90\xb1\x9cv\xa3ݘ,\xa1;\xa5\xee\xe4q9\u0000w\xb9\xb3\u0014\xd9\u0015\x8b\x82nw\ej\x82xkm)\x9aa\xf1\xdd۬\xa2"
all appreciated!
so, reason happening within j
method, in line:
result = javascript.gsub(/(\|<\/|\r\n|\342\200\250|\342\200\251|[\n\r"'])/u) {|match| js_escape_map[match] }
note lovely u
@ end of regex. forced ruby fail match if given string not utf-8 compatible.
now, when encoding string result not meant form of human readable string - purely binary , no encoding display useful. ruby, default, trying display 'utf-8', string not 'utf-8' compatible may contain non-utf-8 sequences (since may contain literally anything).
now solution won't easy string not valid javascript string @ all. also, if managed convert javascript, javascript might save string representation differently, , important thing keep binary information unchanged. easiest way convert encrypted string number:
window.nonce = '<%= @nonce.unpack('b*').first.to_i(2) %>';
it result in number has binary representation same encrypted string. need sure handled everywhere.
question: how string used in javascript?
Comments
Post a Comment