Package conary :: Package lib :: Module sha1helper
[hide private]
[frames] | no frames]

Source Code for Module conary.lib.sha1helper

 1  # 
 2  # Copyright (c) 2004-2008 rPath, Inc. 
 3  # 
 4  # This program is distributed under the terms of the Common Public License, 
 5  # version 1.0. A copy of this license should have been distributed with this 
 6  # source file in a file called LICENSE. If it is not present, the license 
 7  # is always available at http://www.rpath.com/permanent/licenses/CPL-1.0. 
 8  # 
 9  # This program is distributed in the hope that it will be useful, but 
10  # without any warranty; without even the implied warranty of merchantability 
11  # or fitness for a particular purpose. See the Common Public License for 
12  # full details. 
13  # 
14   
15  import sha 
16  import md5 
17  import os 
18  import stat 
19  import struct 
20  from Crypto.Hash import SHA256 
21   
22 -def sha1FileBin(path):
23 oldmode = None 24 mode = os.lstat(path)[stat.ST_MODE] 25 if (mode & 0400) != 0400: 26 oldmode = mode 27 os.chmod(path, mode | 0400) 28 29 fd = os.open(path, os.O_RDONLY) 30 if oldmode is not None: 31 os.chmod(path, oldmode) 32 33 m = sha.new() 34 buf = os.read(fd, 40960) 35 while len(buf): 36 m.update(buf) 37 buf = os.read(fd, 40960) 38 os.close(fd) 39 40 return m.digest()
41
42 -def sha1String(buf):
43 m = sha.new() 44 m.update(buf) 45 return m.digest()
46
47 -def sha1ToString(buf):
48 assert(len(buf) == 20) 49 return "%08x%08x%08x%08x%08x" % struct.unpack("!5I", buf)
50
51 -def sha1FromString(val):
52 assert(len(val) == 40) 53 return struct.pack("!5I", int(val[ 0: 8], 16), 54 int(val[ 8:16], 16), int(val[16:24], 16), 55 int(val[24:32], 16), int(val[32:40], 16))
56
57 -def sha256String(buf):
58 m = SHA256.new() 59 m.update(buf) 60 return m.digest()
61
62 -def sha256ToString(buf):
63 assert(len(buf) == 32) 64 return "%08x%08x%08x%08x%08x%08x%08x%08x" % struct.unpack("!8I", buf)
65
66 -def sha256FromString(val):
67 assert(len(val) == 64) 68 return struct.pack("!8I", int(val[ 0: 8], 16), 69 int(val[ 8:16], 16), int(val[16:24], 16), 70 int(val[24:32], 16), int(val[32:40], 16), 71 int(val[40:48], 16), int(val[48:56], 16), 72 int(val[56:64], 16) )
73
74 -def md5String(buf):
75 m = md5.new() 76 m.update(buf) 77 return m.digest()
78
79 -def md5ToString(buf):
80 assert(len(buf) == 16) 81 return "%08x%08x%08x%08x" % struct.unpack("!4I", buf)
82
83 -def md5FromString(val):
84 assert(len(val) == 32) 85 return struct.pack("!4I", int(val[ 0: 8], 16), 86 int(val[ 8:16], 16), int(val[16:24], 16), 87 int(val[24:32], 16))
88