Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython 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:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old July 29th, 2003, 03:30 PM
theperfectsoup theperfectsoup is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 35 theperfectsoup User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Question XORing string elements?

Hello all,

I'm new to Python and I have a problem that I just can't seem to solve...

Using the sha module I have made a 20-byte string that contains the SHA-1 hash of a file. Now I need to reduce that string a 5-byte string that is a function of the SHA-1 hash. To put it in C++ terms, I'd like to do something like:

unsigned char new_hash[5];
new_hash[0] = sha_hash[0] ^ sha_hash[5] ^ sha_hash[10] ^ sha_hash[15];
new_hash[1] = sha_hash[1] ^ sha_hash[6] ^ sha_hash[11] ^ sha_hash[16];
new_hash[2] = sha_hash[2] ^ sha_hash[7] ^ sha_hash[12] ^ sha_hash[17];
new_hash[3] = sha_hash[3] ^ sha_hash[8] ^ sha_hash[13] ^ sha_hash[18];
new_hash[4] = sha_hash[4] ^ sha_hash[9] ^ sha_hash[14] ^ sha_hash[19];

But XORing only works on ints and longs, and converting any string doesn't rely on its underlying bit pattern (understandably so... e.g., '1' converts to the integer 1, not its underlying bit value 31).

Is this trivial? Any help or ideas would be much appreciated!

Thanks,
- the perfect soup

Reply With Quote
  #2  
Old July 29th, 2003, 04:41 PM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
The trivial reply would be to suggest the use of the builtin 'ord' function. "ord('1')" returns 49, while "hex(ord('1'))" returns 0x31. The other two such convertion functions are 'chr' and the 'oct'.

Reply With Quote
  #3  
Old July 29th, 2003, 05:09 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,529 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 17 h 19 m 5 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
Another useful conversion function, 'str', which convers objects of another type to a string. You could also do this with..

Code:
'this is a string %s' % (other_type)


Have fun,
Mark.

Reply With Quote
  #4  
Old July 29th, 2003, 06:26 PM
theperfectsoup theperfectsoup is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 35 theperfectsoup User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Yay, the ord() trick works... Using chr() I can complte building the new string:

val0 = ord(sha_hash[0]) ^ ord(sha_hash[5]) ^ ord(sha_hash[10]) ^ ord(sha_hash[15])
val1 = ord(sha_hash[1]) ^ ord(sha_hash[6]) ^ ord(sha_hash[11]) ^ ord(sha_hash[16])
val2 = ord(sha_hash[2]) ^ ord(sha_hash[7]) ^ ord(sha_hash[12]) ^ ord(sha_hash[17])
val3 = ord(sha_hash[3]) ^ ord(sha_hash[8]) ^ ord(sha_hash[13]) ^ ord(sha_hash[18])
val4 = ord(sha_hash[4]) ^ ord(sha_hash[9]) ^ ord(sha_hash[14]) ^ ord(sha_hash[19])
new_hash = chr(val0) + chr(val1) + chr(val2) + chr(val3) + chr(val4)

Everything should now go smoothly... Thanks for your help guys!

- the perfect soup

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > XORing string elements?


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 5 hosted by Hostway