Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 6th, 2008, 03:49 AM
sfusion sfusion is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 3 sfusion User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 10 sec
Reputation Power: 0
How would I express this in Ruby?

How would I express this line in Ruby?

PHP
PHP Code:
 $output.= chr(ord(substr($InString$i1)) ^ ($KeyList[$i strlen($Key)])); 


ASP
Code:
strReturn = strReturn & chr( asc(mid(strIn,iInIndex,1)) XOR asc(mid(strKey,iKeyIndex,1)))

Reply With Quote
  #2  
Old June 6th, 2008, 05:55 AM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 641 L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 12 h 26 m 16 sec
Reputation Power: 102
Send a message via AIM to L7Sqr
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.

Reply With Quote
  #3  
Old June 6th, 2008, 06:37 AM
sfusion sfusion is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 3 sfusion User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 10 sec
Reputation Power: 0
Just for a little more background... I am trying to port this function from PHP to RoR.

Heres the original PHP function.

PHP Code:
public function protx_simple_xor($in_string$key
    {
        
$key_list = array(); // --- init key array
        
$output ""// --- init output
        
for($i 0$i strlen($key); $i++) // --- convert key to ascii
        
{
            
$key_list[$i] = ord(substr($key$i1));
         }
        for(
$i 0$i strlen($in_string); $i++) // --- step thru key 1 char
        
{
            
// Get ASCII code from string, get ASCII code from key (loop through with MOD), XOR the two, get the character from the result
            // % is MOD (modulus), ^ is XOR
            
$output .= chr(ord(substr($in_string$i1)) ^ ($key_list[$i strlen($key)]));
        }
        return 
$output;
    } 


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'

Reply With Quote
  #4  
Old June 6th, 2008, 07:33 AM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 641 L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 12 h 26 m 16 sec
Reputation Power: 102
Send a message via AIM to L7Sqr
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
But there are unnecessary steps in there. Consider...
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]
is wasteful since you generate an entire new string to only index that string (of length 1) for a single item. Why not just index the original string?

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])
Comments on this post
sfusion agrees: thanks for your help!

Reply With Quote
  #5  
Old June 6th, 2008, 08:10 AM
sfusion sfusion is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 3 sfusion User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 10 sec
Reputation Power: 0
you sir/madam are my hero of the day!

thanks so much for your help, i've been struggling with this for days!

Reply With Quote
  #6  
Old June 6th, 2008, 01:49 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 3 m 4 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.
Comments on this post
L7Sqr agrees: Nice and elegant.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #7  
Old June 6th, 2008, 02:00 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 3 m 4 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > How would I express this in Ruby?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT