/[theodore]/bunnyblog/modules/dataenc.pyc


UCC Code Repository

Contents of /bunnyblog/modules/dataenc.pyc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations) (download) (as text)
Tue Jan 29 14:32:01 2008 UTC (12 years ago) by svn-admin
File MIME type: application/octet-stream
File size: 33897 byte(s)
Re-import of repository after repository database corruption.

1
2 ,‹Gc@sÌdZdkZdklZdZeo8y$dklZdklZlZWqadZqaXndZ e o\yHdk
3 Z
4 e
5 i ƒdk Tye
6 i eiƒWnej
7 onXWqÊdZ qÊXndd d
8 Zd d d
9 Zdd „Zd „Zd„ZeZd„Zd„Zd„Zd„Zdd„Zdd„Zd„Zd„Ze d„Z!d„Z"de#fd„ƒYZ$d„Z%d„Z&d„Z'd„Z(d„Z)e*d jo e)ƒndS(!s7,
10 DOCS for dataenc as a module
11
12 When run it should go through a few basic tests - see the function test()
13
14 This module provides low-level functions to interleave two bits of data into each other and separate them.
15 It will also encode this binary data to and from ascii - for inclusion in HTML, cookies or email transmission.
16
17 It also provides high level functions to use these functions for time stamping passwords and password hashes,
18 and also to check that a 'time-stamped hash' is both valid and unexpired.
19
20 The check_pass function is interesting. Given an encoded and timestamped hash it compares it with the hash (using SD5) of a password.
21 If it matches *and* is unexpired (you set the time limit) it returns a new encoded time stamp of the hash with the current time.
22 I use this for secure, time limited, logins over CGI. (Could be stored in a cookie as well).
23 (On the first login you will need to compare the password with the stored hash and use that to generate a time stamped hash to include in the page returned.
24 Thereafter you can just use the check_pass function and include the time-stamped hash in a hidden form field for every action.)
25
26 The binary data is interleaved on a 'bitwise' basis - every byte is mangled.
27
28 --
29
30 CONSTANTS
31
32 The main constant defined in dataenc.py is :
33
34 TABLE = '_-0123456789' + 'abcdefghijklmnopqrstuvwxyz'+ 'NOPQRSTUVWXYZABCDEFGHIJKLM'
35 TABLE should be exactly 64 printable characters long... or we'll all die horribly
36 Obviously the same TABLE should be used for decoding as for encoding....
37 note - changing the order of the TABLE here can be used to change the mapping.
38 Versions 1.1.2+ of TABLE uses only characters that are safe to pass in URLs
39 (e.g. using the GET method for passing FORM data)
40
41 OLD_TABLE is the previous encoding map used for versions of dataenc.py previous to 1.1.2
42 See the table_dec function for how to decode data encoded with that map.
43
44 PSYCOIN = 1
45 This decides if we attempt to import psyco or not (the specialising compiler). Set to 0 to not import.
46 If we attempt but fail to import psyco then this value will be set to 0.
47
48 DATEIN = 1
49 As above but for the dateutils and time module.
50 We need to import dateutils for the expired and pass_enc functions (amongst others) to work fully.
51
52
53 FUNCTIONS
54
55 Following are the docstrings extracted from the public functions :
56
57 pass_enc(instring, indict = {}, **keywargs)
58 Returns an ascii version of an SHA hash or a string, with the date/time stamped into it.
59 e.g. For ascii safe storing of password hashes.
60
61 It also accepts the following keyword args (or a dictionary conatining the following keys).
62 (Keywords shown - with default values).
63
64 lower = False, sha_hash = False, daynumber = None, timestamp = None, endleave = False
65
66 Setting lower to True makes instring lowercase before hashing/encoding.
67
68 If sha_hash is set to True then instead of the actual string passed in being encoded, it's SHA hash
69 is encoded. (In either case the string can contain any binary data).
70
71 If a daynumber is passed in then the daynumber will be encoded into the returned string.
72 (daynumber is an integer representing the 'Julian day number' of a date - see the dateutils module).
73 This can be used as a 'datestamp' for the generated code and you can detect anyone reusing old codes this way.
74 If 'daynumber' is set to True then today's daynumber will automatically be used.
75 (dateutils module required - otherwise it will be ignored).
76
77 Max allowed value for daynumber is 16777215 (9th May 41222)
78 (so daynumber can be any integer from 1 to 16777215 that you want to 'watermark' the hash with
79 could be used as a session ID for a CGI for example).
80
81 If a timestamp is passed in it should either be timestamp = True meanining use 'now'.
82 Or it should be a tuple (HOUR, MINUTES).
83 HOUR should be an integer 0-23
84 MINUTES should be an integer 0-59
85
86 The time and date stamp is *binary* interleaved, before encoding, into the data.
87
88 If endleave is set to True then the timestamp is interleaved more securely. Shouldn't be necessary in practise
89 because the stamp is so short and we subsequently encode using table_enc.
90 If the string is long this will slow down the process - because we interleave twice.
91
92
93 pass_dec(incode)
94 Given a string encoded by pass_enc - it returns it decoded.
95 It also extracts the datestamp and returns that.
96 The return is :
97 (instring, daynumber, timestamp)
98
99
100 expired(daynumber, timestamp, validity)
101 Given the length of time a password is valid for, it checks if a daynumber/timestamp tuple is
102 still valid.
103 validity should be an integer tuple (DAYS, HOURS, MINUTES).
104 Returns True for valid or False for invalid.
105 Needs the dateutils module to get the current daynumber.
106
107 unexpired is an alias for expired - because it makes for better tests.
108 (The return results from the expired function are logically the wrong way round, expired returns True if the timestamp is *not* expired..)
109
110
111 check_pass(inhash, pswdhash, EXPIRE)
112 Given the hash (possibly from a webpage or cookie) it checks that it is still valid and matches the password it is supposed to have.
113 If so it returns a new hash - with the current time stamped into it.
114 EXPIRE is a validity tuple to test for (see expired function)
115 e.g. (0, 1, 0) means the supplied hash should be no older than 1 hour
116
117 If the hash is expired it returns -1.
118 If the pass is invalid or doesn't match the supplied pswdhash it returns False.
119 This is a high level function that can do all your password checking and 'time-stamped hash' generation after initial login.
120
121
122 makestamp(daynumber, timestamp)
123 Receives a Julian daynumber (integer 1 to 16777215) and an (HOUR, MINUTES) tuple timestamp.
124 Returns a 5 digit string of binary characters that represent that date/time.
125 Can receive None for either or both of these arguments.
126
127 The function 'daycount' in dateutils will turn a date into a daynumber.
128
129
130 dec_datestamp(datestamp)
131 Given a 5 character datestamp made by makestamp, it returns it as the tuple :
132 (daynumber, timestamp).
133 daynumber and timestamp can either be None *or*
134 daynumber is an integer between 1 and 16777215
135 timestamp is (HOUR, MINUTES)
136
137 The function 'counttodate' in dateutils will turn a daynumber back into a date.
138
139
140 sixbit(invalue)
141 Given a value in it returns a list representing the base 64 version of that number.
142 Each value in the list is an integer from 0-63...
143 The first member of the list is the most significant figure... down to the remainder.
144 Should only be used for positive values.
145
146
147 sixtoeight(intuple)
148 Given four base 64 (6-bit) digits... it returns three 8 bit digits that represent
149 the same value.
150 If length of intuple != 4, or any digits are > 63, it returns None.
151
152 **NOTE**
153 Not quite the reverse of the sixbit function.
154
155
156 table_enc(instring, table=TABLE)
157 The actual function that performs TABLE encoding.
158 It takes instring in three character chunks (three 8 bit values)
159 and turns it into 4 6 bit characters.
160 Each of these 6 bit characters maps to a character in TABLE.
161 If the length of instring is not divisible by three it is padded with Null bytes.
162 The number of Null bytes to remove is then encoded as a semi-random character at the start of the string.
163 You can pass in an alternative 64 character string to do the encoding with if you want.
164
165
166 table_dec(instring, table=TABLE)
167 The function that performs TABLE decoding.
168 Given a TABLE encoded string it returns the original binary data - as a string.
169 If the data it's given is invalid (not data encoded by table_enc) it returns None
170 (definition of invalid : not consisting of characters in the TABLE or length not len(instring) % 4 = 1).
171 You can pass in an alternative 64 character string to do the decoding with if you want.
172
173
174 return_now()
175 Returns the time now.
176 As (HOUR, MINUTES).
177
178
179 binleave(data1, data2, endleave = False)
180 Given two strings of binary data it interleaves data1 into data2 on a bitwise basis
181 and returns a single string combining both. (not just the bytes interleaved).
182 The returned string will be 4 bytes or so longer than the two strings passed in.
183 Use bin_unleave to return the two strings again.
184 Even if both strings passed in are ascii - the result will contain non-ascii characters.
185 To keep ascii-safe you must subsequently encode with table_enc.
186
187 Max length for the smallest data string (one string can be of unlimited size) is about 16meg
188 (increasing this would be easy if anyone needed it - but would be very slow anyway).
189
190 If either string is empty (or the smallest string greater than 16meg) - we return None.
191 The first 4 characters of the string returned 'define' the interleave. (actually the size of the watermark)
192 For added safety you could remove this and send seperately.
193
194 Version 1.0.0 used a bf (bitfield) object from the python cookbook. Version 1.1.0 uses the binary and & and or |
195 operations and is about 2.5 times faster. On my AMD 3000, leaving and unleaving two 20k files took 1.8 seconds.
196 (instead of 4.5 previously - with Psyco enabled this improved to 0.4 seconds.....)
197
198 Interleaving a file with a watermark of pretty much any size makes it unreadable - this is because *every* byte is changed.
199 (Except perhaps a few at the end - see the endleave keyword). However it shouldn't be relied on if you need
200 a really secure method of encryption. For many purposes it will be sufficient however.
201
202 In practise any file not an exact multiple of the size of the watermark will have a chunk at the end that is untouched.
203 To get round this you can set endleave = True.. which then releaves the end data back into itself.
204 (and therefore takes twice as long - it shouldn't be necessary where you have a short watermark.)
205
206 data2 ought to be the smaller string - or they will be swapped round internally.
207 This could cause you to get them back in an unexpected order from binunleave.
208
209
210 binunleave(data)
211 Given a chunk of data woven by binleave - it returns the two seperate pieces of data.
212
213
214 For the binary operations of binleave and binunleave, version 1.0.0 used a bf (bitfield) object from
215 the python cookbook.
216
217 class bf(object)
218 the bf(object) from activestate python cookbook - by Sebastien Keim - Many Thanks
219 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/113799
220
221 Version 1.1.0 replaced these with specific binary AND & and OR | operations that are about 2.5 times faster.
222 They are 'inline' in the functions for speed (avoiding function calls) but are available separately as well.
223
224 def bittest(value, bitindex)
225 This function returns the setting of any bit from a value.
226 bitindex starts at 0.
227
228 def bitset(value, bitindex, bit)
229 Sets a bit, specified by bitindex, in in 'value' to 'bit'.
230 bit should be 1 or 0
231
232
233
234 There are also the 'private functions' which actually contain the substance of binleave and binunleave,
235 You are welcome to 'browse' them - but you shouldn't need to use them directly.
236
237
238 Any comments, suggestions and bug reports welcome.
239
240 Regards,
241
242 Fuzzy
243
244 michael AT foord DOT me DOT uk
245
246
247 N(srandomi(sstrftime(sdaycounts
248 returndatei(t*s _-0123456789tabcdefghijklmnopqrstuvwxyztNOPQRSTUVWXYZABCDEFGHIJKLMs !$%^&*()_-+=c
249 Ks¼|djo
250 h}nhdt<dt<dd<dd<dt<}| o|o
251 |}nx0|D](} |i| ƒp|| || <qjqjW|do|iƒ}n|dot i
252 |ƒi ƒ}n|dt joAt pd|d<q'tƒ\}}}t|||ƒ|d<n|dt jo&t pd|d<q^tƒ|d<nt|d|dƒ}t|ƒt|ƒjo|d}nt|||dƒ}t|ƒS(skReturns an ascii version of an SHA hash or a string, with the date/time stamped into it.
253 e.g. For ascii safe storing of password hashes.
254
255 It also accepts the following keyword args (or a dictionary conatining the following keys).
256 (Keywords shown - with default values).
257
258 lower = False, sha_hash = False, daynumber = None, timestamp = None, endleave = False
259
260 Setting lower to True makes instring lowercase before hashing/encoding.
261
262 If sha_hash is set to True then instead of the actual string passed in being encoded, it's SHA hash
263 is encoded. (In either case the string can contain any binary data).
264
265 If a daynumber is passed in then the daynumber will be encoded into the returned string.
266 (daynumber is an integer representing the 'Julian day number' of a date - see the dateutils module).
267 This can be used as a 'datestamp' for the generated code and you can detect anyone reusing old codes this way.
268 If 'daynumber' is set to True then today's daynumber will automatically be used.
269 (dateutils module required - otherwise it will be ignored).
270
271 Max allowed value for daynumber is 16777215 (9th May 41222)
272 (so daynumber can be any integer from 1 to 16777215 that you want to 'watermark' the hash with
273 could be used as a session ID for a CGI for example).
274
275 If a timestamp is passed in it should either be timestamp = True meanining use 'now'.
276 Or it should be a tuple (HOUR, MINUTES).
277 HOUR should be an integer 0-23
278 MINUTES should be an integer 0-59
279
280 The time and date stamp is *binary* interleaved, before encoding, into the data.
281
282 If endleave is set to True then the timestamp is interleaved more securely. Shouldn't be necessary in practise
283 because the stamp is so short and we subsequently encode using table_enc.
284 If the string is long this will slow down the process - because we interleave twice.
285 tlowertsha_hasht daynumbert timestamptendleaves &mjf-end;N(tindicttNonetFalsetarglisttkeywargstkeywordthas_keytinstringRtshatnewtdigesttTruetDATEINt
286 returndatetatbtctdaycountt
287 return_nowt makestampt datestamptlentbinleavetoutdatat table_enc(
288 RRR RRR RRRR ((t./modules/dataenc.pytpass_encQs8"
289 3
290   cCs¨t|ƒ}t|ƒ\}}t|ƒdjo.|}|idƒo|d }n|}n+|}|idƒo|d }n|}t
291 |ƒ\}}|||fS(s®Given a string encoded by pass_enc - it returns it decoded.
292 It also extracts the datestamp and returns that.
293 The return is :
294 (instring, daynumber, timestamp)
295 is &mjf-end;i÷ÿÿÿN( t table_dectincodetbinaryt
296 binunleavetout1tout2RRtendswithRt dec_datestampRR(R$R%R(RR'RRR((R!tpass_dec•s 
297 cCsp|p
298 tdƒ}|pd
299 }tptdƒ‚n|\}}|\}} }t ƒ\}}
300 } t||
301 | ƒ}tƒ\} } || }||}||}x&|djo|d7}|d8}q¦Wx&|djo|d7}|d8}qÏW||7}||jotSn||jotSn| |jotSn| |jotSn| |jotSntSd S( saGiven the length of time a password is valid for, it checks if a daynumber/timestamp tuple is
302 still valid.
303 validity should be an integer tuple (DAYS, HOURS, MINUTES).
304 Returns True for valid or False for invalid.
305 Needs the dateutils module to get the current daynumber.
306
307 >>> a, b, c = returndate()
308 >>> today = daycount(a, b, c)
309 >>> h, m = return_now()
310 >>> expired(today, (h, m-2), (0,0,1))
311 False
312 >>> expired(today, (h, m-2), (0,0,10))
313 True
314 >>> expired(today, (h-2, m-2), (0,1,10))
315 False
316 >>> expired(today-1, (h-2, m-2), (1,1,10))
317 False
318 >>> expired(today-1, (h-2, m-2), (2,1,10))
319 True
320 >>>
321 t0is8Need the dateutils module to use the 'expired' function.i;ii<iiN(ii(RtintRRt ImportErrorth1tm1tvaliditytd2th2tm2RRRRRttodayRthtmR
322 R(RRR1R/R2R0R4R5RRRR6R7R3((R!texpired¬sD  
323 
324 
325  
326  
327 
328      cCs¨|ptdƒd}nI|d}|d}|d}|d}t|ƒt|ƒt|ƒ}|p|tdƒd}n#|t|dƒt|dƒ}|S( s:Receives a Julian daynumber (integer 1 to 16777215) and an (HOUR, MINUTES) tuple timestamp.
329 Returns a 5 digit string of binary characters that represent that date/time.
330 Can receive None for either or both of these arguments.
331
332 The function 'daycount' in dateutils will turn a date into a daynumber.
333 iiiiiÿiiN(RtchrRtday1tday2R(RRR;R:R((R!Rðs
334 
335 
336 
337  "cCsª|d }|d}t|dƒdt|dƒdt|dƒ}|djo
338 d}nt|dƒdjo
339 d}n!t|dƒt|dƒf}||fS( sGGiven a 5 character datestamp made by makestamp, it returns it as the tuple :
340 (daynumber, timestamp).
341 daynumber and timestamp can either be None *or*
342 daynumber is an integer between 1 and 16777215
343 timestamp is (HOUR, MINUTES)
344
345 The function 'counttodate' in dateutils will turn a daynumber back into a date.iiiiiiiÿN(RRt timecharstordR R(RRRR<((R!R*s
346 
347 4
348 
349  cCs¨|djo dgSnd}g}d}x+||jo|d7}td|ƒ}q-WxI|oA|d8}|it|td|ƒƒƒ|td|ƒ}q[W|S(sGiven a value in it returns a list representing the base 64 version of that number.
350 Each value in the list is an integer from 0-63...
351 The first member of the list is the most significant figure... down to the remainder.
352 Should only be used for positive values.
353 iiiÿÿÿÿi@N(tinvaluetpowertoutlistttesttpowtappendR-(R>R@R?RA((R!tsixbits    
354 
355  cCs¤t|ƒdjod Snx#|D]}|djod Sq"q"W|d|dd|dd|dd }|d
356 }|d
357 }|d }|d }|||fS( síGiven four base 64 (6-bit) digits... it returns three 8 bit digits that represent
358 the same value.
359 If length of intuple != 4, or any digits are > 63, it returns None.
360
361 **NOTE**
362 Not quite the reverse of the sixbit function.ii?iii@iiiiiiN(RtintupleR tentrytvaluetval1tval2(RERGRIRFRH((R!t
363 sixtoeight/s  .
364 
365 
366 
367 cCs¥|d jo
368 t}ng}t|ƒd}|o|tdƒd|}nx­|o¥|d }|d}dt |dƒdt |dƒt |dƒ}t |ƒ}x(t|ƒdjo|i ddƒq±Wx|D]}|i||ƒqàWqSW|p%|i d|ttƒdƒƒnm|djo)|i d|ttƒdƒdƒn7|djo)|i d|ttƒd ƒd
369 ƒnd i|ƒS( sThe actual function that performs TABLE encoding.
370 It takes instring in three character chunks (three 8 bit values)
371 and turns it into 4 6 bit characters.
372 Each of these 6 bit characters maps to a character in TABLE.
373 If the length of instring is not divisible by three it is padded with Null bytes.
374 The number of Null bytes to remove is then encoded as a semi-random character at the start of the string.
375 You can pass in an alternative 64 character string to do the encoding with if you want.
376 iiiiiiiiii*tN(ttableR tTABLEtoutRRRAR9tchunkR=RGRDtnewdattinserttcharRCR-trandomtjoin(RRLRORPRRRGRARN((R!R Bs4
377 
378 
379 4 % ) )c Cs[|d jo
380 t}ng}|i|dƒ}|djod Sn|d}t|ƒddjod Snx§|oŸ|d }|d}g}x?|D]7}|i|ƒ}|djod Sn|i |ƒq™Wt |ƒ}|pd Snx!|D]}|i t|ƒƒqöWqqW|djo|d }n|djo|d }ndi|ƒS(
381 s¢The function that performs TABLE decoding.
382 Given a TABLE encoded string it returns the original binary data - as a string.
383 If the data it's given is invalid (not data encoded by table_enc) it returns None
384 (definition of invalid : not consisting of characters in the TABLE or length not len(instring) % 4 = 1).
385 You can pass in an alternative 64 character string to do the decoding with if you want.
386 iiÿÿÿÿiii)iiþÿÿÿRKN(RLR RMRNtfindRtrem_testRROtnewchunkRRRARCRJtnewcharsR9RT( RRLRORRRWRVRARXRN((R!R#as@
387  
388 
389 
390     cCs"ttdƒƒttdƒƒfS(s-Returns the time now.
391 As (HOUR, MINUTES).s%Is%MN(R-tstrftime(((R!R€scCswyet|ƒ\}}}t|ƒ|jptSnt|||ƒpdSnt
392 |dt dt ƒSWn tSnXdS(sìGiven the hash (possibly from a webpage) it checks that it is still valid and matches the password it is supposed
393 to have.
394 If so it returns the new hash.
395 If expired it returns -1.
396 If the pass is invalid it returns False.iÿÿÿÿRRN( R+tinhashRRRR#tpswdhashR
397 t unexpiredtEXPIRER"R(RZR[R]RRR((R!t
398 check_pass…scCs£t||ƒ\}}}tttƒdƒƒ|}|o[|oTt |ƒdjoAt|||ƒ\}}}tttƒdƒdƒ|}n|||S(s)Given two strings of binary data it interleaves data1 into data2 on a bitwise basis
399 and returns a single string combining both. (bits interleaved not just the bytes).
400 The returned string will be 4 bytes or so longer than the two strings passed in.
401 Use bin_unleave to return the two strings again.
402 Even if both strings passed in are ascii - the result will contain non-ascii characters.
403 To keep ascii-safe you must subsequently encode with table_enc.
404
405 Max length for the smallest data string (one string can be of unlimited size) is about 16meg
406 (increasing this would be easy if anyone needed it - but would be very slow anyway).
407
408 If either string is empty (or the smallest string greater than 16meg) - we return None.
409 The first 4 characters of the string returned 'define' the interleave. (actually the size of the watermark)
410 For added safety you could remove this and send seperately.
411
412 Version 1.0.0 used a bf (bitfield) object from the python cookbook. Version 1.1.0 uses the binary and & and or |
413 operations and is about 2.5 times faster. On my AMD 3000, leaving and unleaving two 20k files took 1.8 seconds.
414 (instead of 4.5 previously - with Psyco enabled this improved to 0.4 seconds.....)
415
416 Interleaving a file with a watermark of pretty much any size makes it unreadable - this is because *every* byte is changed.
417 (Except perhaps a few at the end - see the endleave keyword). However it shouldn't be relied on if you need
418 a really secure method of encryption. For many purposes it will be sufficient however.
419
420 In practise any file not an exact multiple of the size of the watermark will have a chunk at the end that is untouched.
421 To get round this you can set endleave = True.. which then releaves the end data back into itself.
422 (and therefore takes twice as long - it shouldn't be necessary where you have a short watermark.)
423
424 data2 ought to be the smaller string - or they will be swapped round internally.
425 This could cause you to get them back in an unexpected order from binunleave.
426 i€iN(
427 t internalfunctdata1tdata2theaderRNR9R-RSRR(R`RaRRbRN((R!R–s!%cCsm|d}|d}t|ƒ\}}t|ƒdjo*||}|d}t|ƒ\}}n||fS(sUGiven a chunk of data woven by binleave - it returns the two seperate pieces of data.iiiN(tdataRbt internalfunc2R`RaR=(RcRbR`Ra((R!R&¼s
428 
429 
430 
431 tbfcBsGtZdZdd„Zd„Zd„Zd„Zd„Zd„ZRS(s”the bf(object) from activestate python cookbook - by Sebastien Keim - Many Thanks
432 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/113799icCs ||_dS(N(RGtselft_d(RfRG((R!t__init__ÑscCs|i|?d@S(Ni(RfRgtindex(RfRi((R!t __getitem__ÔscCs1|d@|>}d|>}|i|@|B|_dS(Nl(RGRitmaskRfRg(RfRiRGRk((R!t __setitem__×s
433 cCs!d||d}|i|?|@S(Nli(tendtstartRkRfRg(RfRnRmRk((R!t __getslice__ÜscCsNd||d}||@|>}||>}|i|@|B|_|i|?|@S(Nli(RmRnRkRGRfRg(RfRnRmRGRk((R!t __setslice__às
434 
435 cCs|iS(N(RfRg(Rf((R!t__int__çs( t__name__t
436 __module__t__doc__RhRjRlRoRpRq(((R!ReÎs      cCs|d|>@|?S(sYThis function returns the setting of any bit from a value.
437 bitindex starts at 0.
438 iN(RGtbitindex(RGRu((R!tbittestëscCs%|d@|>}d|>}||@|BS(soSets a bit, specified by bitindex, in 'value' to 'bit'.
439 bit should be 1 or 0
440 bitindex starts at 0.
441 lN(tbitRuRkRG(RGRuRwRk((R!tbitsetñs
442 cCs~t|ƒt|ƒjo|}|}|}n| p| odSnt|ƒ}|tddƒjodSnt|ƒ|}|djo
443 d}n|d}|d} | d} | d} t |ƒt | ƒt | ƒ}g} t|ƒD]}| t|ƒqñ~ }d}g} t|ƒD]}| t|ƒq$~ }dddd d
444 d d d g}g}
445 |d d }xÅ|o½||||!} ||}d}|idƒ}| idƒ}d}d}d}d}x;||jo-|d jo#|
446 it |ƒƒd}d}n||dpF|||@o|||B}n|d7}|d7}|d7}qÖn|djoX| oQ|||@o|||B}n| idƒ}d}|d7}|d7}qÖn|||@o|||B}n|d7}|d7}|d7}qÖW|d jo|
447 it |ƒƒququW|di|
448 ƒdig} ||D]}| t |ƒq^~ ƒfS(sZUsed by binleave.
449 This function interleaves data2 into data1 a little chunk at a time.iiiÿÿiiiiiiii i@i€iRKN( RRaR`tdummyR tlengthRBtmultipletheader1theader3theader2R9Rbt_[1]tlistRRR=tstartpostBINLISTRNtbitlent chunklisttheapobjtpoptmainobjtcharobjRut mainindext heapindext charindexRCRT(R`RaRuR‹RŠRRRbR…RƒR„RNRR~R}R|RR{R‰RyR‡RˆR‚Rz((R!R_s€
450  
451 
452 
453 
454 
455  --
456   
457 
458 
459 
460 
461 
462 
463 
464  cCs|d }t|dƒ}t|dƒdt|dƒdt|dƒ}t|ƒ|}||d} | djo
465 d} n| d}g}g}
466 d} ddddd d
467 d d g}x!| |jo| d7} || }|| }g}|D]}|t|ƒqö~} d}d}| idƒ} d}d}d}d}x]||joO|djo#|
468 it|ƒƒd}d}n|| pl| ||@o|||B}n|d jp|d7}n| idƒ} d}|d7}|d7}qHn|d joX| oQ| ||@o|||B}n| idƒ} d}|d7}|d7}qHn| ||@o|||B}n|d7}|d7}|d7}qHW|djo|
469 it|ƒƒn|it|ƒƒqÀWdi|ƒdi|
470 |ƒfS(s]Used by binunleave.
471 This function extracts data that has been interleaved using binleave.iiiiiiiiii i@i€iRKN(RctlenstrR€R=tlength2Rtlength1R{RƒR'RNRiR‚RORRRR„R…toutbyteR†RˆRuR‰RŠR‹RCR9RT(RcRuRŠRŒRORRR‹R…RƒR„RNRiRˆR{R‰RR'RRŽR‚R((R!RdCsv
472 4
473 
474  
475 
476 
477 '  
478   
479 
480 
481 
482 
483 
484  cCs™dkl}dkl}dGHd}x<|ttƒjo(t|Gtit|ƒGH|d7}q(WdGHx~tovt dƒ}
485 |
486 pPnt |
487 ƒ}t |ƒ}|GHdGt|ƒGd Gt|ƒdd
488 GHd G|GHd Gt|ƒGHqlWd GHxRtoJt dƒ}|pPnt|dtdtdtƒ}|GHt|ƒGHqòWdGHHdGd} d} d} xV| djoH| d7} | tttƒdƒƒ} | tttƒdƒƒ} qcWx9| djo+| d7} | tttƒdƒƒ} q¼W|ƒ} t| | tƒ}t|ƒGHt|ƒ\}}dGt|ƒ| ƒd GdGHdGH|| jGH|| jGH|dƒo |dƒoüHdGHdGHdGd GHt dd!ƒ} | i!ƒ} | i"ƒt dd!ƒ} | i!ƒ} | i"ƒ|ƒ} t| | tƒ}t|ƒGHt d"d#ƒ} | i#|ƒ| i"ƒt|ƒ\}}t|ƒ| ƒd GHt d$d#ƒ} | i#|ƒ| i"ƒnHd%GHd&GHd'GHd(Gd)GHdS(*N(sclock(sexistssPrinting the TABLE : iisS
489 Enter test password to encode using table_enc :
490 (Hit enter to continue past this)
491 s>>...s
492 length : s modulo 4 of length - 1 : is
493 Decoded : s Length dec : sS
494 Enter password - to timestamp and then encode :
495 (Hit enter to continue past this)
496 RRRsQ
497
498 Testing interleaving a 1000 byte random string with a 1500 byte random string :s$Overall length of combined string : RKièiiÜs+Time taken (including print statements ;-) is secondss6Test for equality of extracted data against original :s test1.zips test2.zips#Reading 'test1.zip' and 'test2.zip'sKInterleaving them together and writing the combined file out as 'test3.zip'sCThen unleaving them and writing 'test1.zip' back out as 'test4.zip's* to confirm it is unchanged by the processtrbs test3.ziptwbs test4.zipsUnable to perform final test.sCWe need two files to use for the test : 'test1.zip' and 'test2.zip'sPWe then interleave them together, and write the combined file out as 'test3.zip'sIThen we unleave them again, and write 'test1.zip' back out as 'test4.zip's7(So we can confirm that it's unchanged by the process.)($ttimetclocktos.pathtexistsRiRRMRURt raw_inputRyR RAR#ttest2RR"R
499 tcodeR+RRRR9R-RStdRR&ta1ta2tstrtfiletreadtclosetwrite(R˜RR—RiR•R“RARšR›RRyRRR™((R!RAƒs¢     %   
500 " 
501 "     
502  
503    
504  t__main__(+RtRRSRR’RYt dateutilsRRtPSYCOONtpsycotfullt psyco.classest cannotcompiletretcompilet NameErrorRMt OLD_TABLER R"R+R8R\RR*RDRJR R#RR^R
505 RR&tobjectReRvRxR_RdRARr(RvRSReR¤R#R\R^R*RRRARMRR&RDRR R£RxR8R+R«RR"RJRRdRRYR_((R!t?$sZ    
506    D  B         &    C @ ] ~

Properties

Name Value
svn:mime-type application/octet-stream

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26