Exemplo n.º 1
0
 def test_compute_file_sha(self):
     f = StringIO('abcd1234wxyz')
     self.assertEqual(make_sha('abcd1234wxyz').hexdigest(),
                      compute_file_sha(f).hexdigest())
     self.assertEqual(make_sha('abcd1234wxyz').hexdigest(),
                      compute_file_sha(f, buffer_size=5).hexdigest())
     self.assertEqual(make_sha('abcd1234').hexdigest(),
                      compute_file_sha(f, end_ofs=-4).hexdigest())
     self.assertEqual(make_sha('1234wxyz').hexdigest(),
                      compute_file_sha(f, start_ofs=4).hexdigest())
     self.assertEqual(
       make_sha('1234').hexdigest(),
       compute_file_sha(f, start_ofs=4, end_ofs=-4).hexdigest())
Exemplo n.º 2
0
 def test_compute_file_sha(self):
     f = StringIO('abcd1234wxyz')
     self.assertEqual(make_sha('abcd1234wxyz').hexdigest(),
                      compute_file_sha(f).hexdigest())
     self.assertEqual(make_sha('abcd1234wxyz').hexdigest(),
                      compute_file_sha(f, buffer_size=5).hexdigest())
     self.assertEqual(make_sha('abcd1234').hexdigest(),
                      compute_file_sha(f, end_ofs=-4).hexdigest())
     self.assertEqual(make_sha('1234wxyz').hexdigest(),
                      compute_file_sha(f, start_ofs=4).hexdigest())
     self.assertEqual(
       make_sha('1234').hexdigest(),
       compute_file_sha(f, start_ofs=4, end_ofs=-4).hexdigest())
Exemplo n.º 3
0
def obj_sha(type, chunks):
    """Compute the SHA for a numeric type and object chunks."""
    sha = make_sha()
    sha.update(object_header(type, chunks_length(chunks)))
    for chunk in chunks:
        sha.update(chunk)
    return sha.digest()
Exemplo n.º 4
0
 def sha(self):
     """The SHA1 object that is the name of this object."""
     if self._sha is None or self._needs_serialization:
         # this is a local because as_raw_chunks() overwrites self._sha
         new_sha = make_sha()
         new_sha.update(self._header())
         for chunk in self.as_raw_chunks():
             new_sha.update(chunk)
         self._sha = new_sha
     return self._sha
Exemplo n.º 5
0
 def sha(self):
     """The SHA1 object that is the name of this object."""
     if self._sha is None or self._needs_serialization:
         # this is a local because as_raw_chunks() overwrites self._sha
         new_sha = make_sha()
         new_sha.update(self._header())
         for chunk in self.as_raw_chunks():
             new_sha.update(chunk)
         self._sha = new_sha
     return self._sha
Exemplo n.º 6
0
 def test_write_pack_object_sha(self):
     f = StringIO()
     f.write('header')
     offset = f.tell()
     sha_a = make_sha('foo')
     sha_b = sha_a.copy()
     write_pack_object(f, Blob.type_num, 'blob', sha=sha_a)
     self.assertNotEqual(sha_a.digest(), sha_b.digest())
     sha_b.update(f.getvalue()[offset:])
     self.assertEqual(sha_a.digest(), sha_b.digest())
Exemplo n.º 7
0
 def test_write_pack_object_sha(self):
     f = StringIO()
     f.write('header')
     offset = f.tell()
     sha_a = make_sha('foo')
     sha_b = sha_a.copy()
     write_pack_object(f, Blob.type_num, 'blob', sha=sha_a)
     self.assertNotEqual(sha_a.digest(), sha_b.digest())
     sha_b.update(f.getvalue()[offset:])
     self.assertEqual(sha_a.digest(), sha_b.digest())
Exemplo n.º 8
0
def iter_sha1(iter):
    """Return the hexdigest of the SHA1 over a set of names.

    :param iter: Iterator over string objects
    :return: 40-byte hex sha1 digest
    """
    sha1 = make_sha()
    for name in iter:
        sha1.update(name)
    return sha1.hexdigest()
Exemplo n.º 9
0
 def __init__(self, read_all, read_some=None):
     self.read_all = read_all
     if read_some is None:
         self.read_some = read_all
     else:
         self.read_some = read_some
     self.sha = make_sha()
     self._offset = 0
     self._rbuf = StringIO()
     # trailer is a deque to avoid memory allocation on small reads
     self._trailer = deque()
Exemplo n.º 10
0
    def calculate_checksum(self):
        """Calculate the checksum for this pack.

        :return: 20-byte binary SHA1 digest
        """
        s = make_sha()
        self._file.seek(0)
        todo = self._get_size() - 20
        while todo > 0:
            x = self._file.read(min(todo, 1<<16))
            s.update(x)
            todo -= len(x)
        return s.digest()
Exemplo n.º 11
0
 def _make_sha(self):
     ret = make_sha()
     ret.update(self._header())
     for chunk in self.as_raw_chunks():
         ret.update(chunk)
     return ret
Exemplo n.º 12
0
 def _make_sha(self):
     ret = make_sha()
     ret.update(self._header())
     for chunk in self.as_raw_chunks():
         ret.update(chunk)
     return ret
Exemplo n.º 13
0
    def calculate_checksum(self):
        """Calculate the SHA1 checksum over this pack index.

        :return: This is a 20-byte binary digest
        """
        return make_sha(self._contents[:-20]).digest()
Exemplo n.º 14
0
 def __init__(self, f):
     self.f = f
     self.sha1 = make_sha("")