|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
How would I express this in Ruby?
How would I express this line in Ruby?
PHP PHP Code:
ASP Code:
strReturn = strReturn & chr( asc(mid(strIn,iInIndex,1)) XOR asc(mid(strKey,iKeyIndex,1))) |
|
#2
|
|||
|
|||
|
Code:
output << (str[i] ^ keylist[i % key.size]).chr Of course, this is just a guess. I know nothing of PHP or ASP, I am only inferring what I can from the syntax of your examples.
__________________
-- I'll provide you with reference points; if they dont work, refer to something else. If you process text, this might make your life a little easier. |
|
#3
|
|||
|
|||
|
Just for a little more background... I am trying to port this function from PHP to RoR.
Heres the original PHP function. PHP Code:
And heres my attempted port. Code:
def simple_xor(str = "test", key = ENCRYPTION_PASSWORD) key_list = Array.new out = "" key.size.times do |i| key_list[key_list.size + 1] = key.slice(i, 1).to_s[0] end str.size.times do |i| ## problem lies here somewhere out << ((str.slice(i, 1))[0] ^ (key_list[i % key.size])).chr ## PHP: $output.= chr(ord(substr($InString, $i, 1)) ^ ($KeyList[$i % strlen($Key)])); ## ASP: strReturn = strReturn & chr( asc(mid(strIn,iInIndex,1)) XOR asc(mid(strKey,iKeyIndex,1))) end @out = out end the error i'm currently getting is 'no implicit conversion from nil to integer' |
|
#4
|
|||
|
|||
|
This follows exactly your code...
Code:
#!/usr/bin/env ruby
key_list, output = [], ""
key, str = "key", "input string"
key.size.times { |i| key_list << key[i] }
str.size.times do |i|
output << (str[i] ^ key_list[i % key.size]).chr
end
Code:
output = ""
key, str = "key", "input string"
str.size.times { |i| output << (str[i] ^ key[i % key.size]).chr }
Also, this type of stuff Code:
str.slice(i, 1))[0] As a side note, yout nil conversion seems to be coming from teh fact that you are filling the array starting at index 1 (key_list[key_list.size + 1]) but when you loop through and use the values later, you try to access index 0 (key_list[i % key.size]) |
|
#5
|
|||
|
|||
|
you sir/madam are my hero of the day!
![]() thanks so much for your help, i've been struggling with this for days! |
|
#6
|
||||
|
||||
|
Alternatively, if you don't want to manually push characters onto a string one by one you could use map&join together.
Code:
k, s = "key", "string"
(0...s.size).map{ |i| (s[i] ^ k[i % k.size]).chr }.join
Take care, Mark. |
|
#7
|
||||
|
||||
|
If that looks a little messy to you then you can always abstract the selection processes into something prettier, ala:
Code:
map("string", "key") { |a, b| (a ^ b).chr }.join
Which is enabled by Code:
def map(a, b)
(0 ... a.size).map do |i|
yield(a[i], b[i % b.size])
end
end
You could make this more generally useful by ensuring that it maps over the largest of the input enumerable Code:
def map(a, b)
size = [a.size, b.size].max
(0 ... size).map do |i|
yield(a[i % a.size], b[i % b.size])
end
end
If you wanted to take this further you could also make it an n-ary method and put it in a mixin class. Anyway, enjoy. Mark. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Ruby Programming > How would I express this in Ruby? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|