def move_in_thin_pack(self, path): """Move a specific file containing a pack into the pack directory. :note: The file should be on the same file system as the packs directory. :param path: Path to the pack file. """ data = PackData(path) # Write index for the thin pack (do we really need this?) temppath = os.path.join(self.pack_dir, sha_to_hex(urllib2.randombytes(20))+".tempidx") data.create_index_v2(temppath, self.get_raw) p = Pack.from_objects(data, load_pack_index(temppath)) # Write a full pack version temppath = os.path.join(self.pack_dir, sha_to_hex(urllib2.randombytes(20))+".temppack") write_pack(temppath, ((o, None) for o in p.iterobjects(self.get_raw)), len(p)) pack_sha = load_pack_index(temppath+".idx").objects_sha1() newbasename = os.path.join(self.pack_dir, "pack-%s" % pack_sha) os.rename(temppath+".pack", newbasename+".pack") os.rename(temppath+".idx", newbasename+".idx") self._add_known_pack(newbasename)
def move_in_thin_pack(self, path): """Move a specific file containing a pack into the pack directory. :note: The file should be on the same file system as the packs directory. :param path: Path to the pack file. """ p = PackData(path) temppath = os.path.join(self.pack_dir(), sha_to_hex(urllib2.randombytes(20))+".temppack") write_pack(temppath, p.iterobjects(self.get_raw), len(p)) pack_sha = PackIndex(temppath+".idx").objects_sha1() os.rename(temppath+".pack", os.path.join(self.pack_dir(), "pack-%s.pack" % pack_sha)) os.rename(temppath+".idx", os.path.join(self.pack_dir(), "pack-%s.idx" % pack_sha))
def modify(self, contents=None): if contents is None: contents = urllib2.randombytes(100) txdelta = self.file.apply_textdelta() delta.send_stream(StringIO(contents), txdelta)
def get_cnonce(self, nonce): dig = hashlib.sha1("%s:%s:%s:%s" % (self.nonce_count, nonce, time.ctime(), randombytes(8))).hexdigest() return dig[:16]
def build_digest_response(self, fields, username, password): """ Takes a Proxy-Authenticate: Digest header and creates a response header :param fields: The string portion of the Proxy-Authenticate header after "Digest " :param username: The username to use for the response :param password: The password to use for the response :return: None if invalid Proxy-Authenticate header, otherwise the string of fields for the Proxy-Authorization: Digest header """ fields = urllib2.parse_keqv_list(urllib2.parse_http_list(fields)) realm = fields.get('realm') nonce = fields.get('nonce') qop = fields.get('qop') algorithm = fields.get('algorithm') if algorithm: algorithm = algorithm.lower() opaque = fields.get('opaque') if algorithm in ['md5', None]: def md5hash(string): return hashlib.md5(string).hexdigest() hash = md5hash elif algorithm == 'sha': def sha1hash(string): return hashlib.sha1(string).hexdigest() hash = sha1hash else: return None host_port = u"%s:%s" % (self.host, self.port) a1 = "%s:%s:%s" % (username, realm, password) a2 = "CONNECT:%s" % host_port ha1 = hash(a1) ha2 = hash(a2) if qop == None: response = hash(u"%s:%s:%s" % (ha1, nonce, ha2)) elif qop == 'auth': nc = '00000001' cnonce = hash(urllib2.randombytes(8))[:8] response = hash(u"%s:%s:%s:%s:%s:%s" % (ha1, nonce, nc, cnonce, qop, ha2)) else: return None response_fields = { 'username': username, 'realm': realm, 'nonce': nonce, 'response': response, 'uri': host_port } if algorithm: response_fields['algorithm'] = algorithm if qop == 'auth': response_fields['nc'] = nc response_fields['cnonce'] = cnonce response_fields['qop'] = qop if opaque: response_fields['opaque'] = opaque return ', '.join([ u"%s=\"%s\"" % (field, response_fields[field]) for field in response_fields ])
def build_digest_response(self, fields, username, password): """ Takes a Proxy-Authenticate: Digest header and creates a response header :param fields: The string portion of the Proxy-Authenticate header after "Digest " :param username: The username to use for the response :param password: The password to use for the response :return: None if invalid Proxy-Authenticate header, otherwise the string of fields for the Proxy-Authorization: Digest header """ fields = urllib2.parse_keqv_list(urllib2.parse_http_list(fields)) realm = fields.get("realm") nonce = fields.get("nonce") qop = fields.get("qop") algorithm = fields.get("algorithm") if algorithm: algorithm = algorithm.lower() opaque = fields.get("opaque") if algorithm in ["md5", None]: def md5hash(string): return hashlib.md5(string).hexdigest() hash = md5hash elif algorithm == "sha": def sha1hash(string): return hashlib.sha1(string).hexdigest() hash = sha1hash else: return None host_port = u"%s:%s" % (self.host, self.port) a1 = "%s:%s:%s" % (username, realm, password) a2 = "CONNECT:%s" % host_port ha1 = hash(a1) ha2 = hash(a2) if qop == None: response = hash(u"%s:%s:%s" % (ha1, nonce, ha2)) elif qop == "auth": nc = "00000001" cnonce = hash(urllib2.randombytes(8))[:8] response = hash(u"%s:%s:%s:%s:%s:%s" % (ha1, nonce, nc, cnonce, qop, ha2)) else: return None response_fields = { "username": username, "realm": realm, "nonce": nonce, "response": response, "uri": host_port, } if algorithm: response_fields["algorithm"] = algorithm if qop == "auth": response_fields["nc"] = nc response_fields["cnonce"] = cnonce response_fields["qop"] = qop if opaque: response_fields["opaque"] = opaque return ", ".join([u'%s="%s"' % (field, response_fields[field]) for field in response_fields])