@@ -26,7 +26,6 @@ import tempfile
2626import os
2727import sys
2828import re
29- import hashlib
3029import argparse
3130import enum
3231import ctypes
@@ -46,6 +45,9 @@ plugin.encryptrefname.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
4645plugin .decryptrefname .argtypes = [ctypes .c_void_p , ctypes .c_void_p ]
4746plugin .set_option .argtypes = [ctypes .c_char_p , ctypes .c_size_t ,
4847 ctypes .c_char_p ]
48+ plugin .hashdata .argtypes = [ctypes .c_void_p , ctypes .c_size_t , ctypes .c_void_p ]
49+ plugin .hashdatahex .argtypes = [ctypes .c_void_p , ctypes .c_size_t ]
50+ plugin .hashdatahex .restype = ctypes .c_char_p
4951
5052if not hasattr (pygit2 .enums , 'FileMode' ):
5153 class FileMode (enum .IntFlag ):
@@ -118,6 +120,19 @@ def decryptrefname(ref, key):
118120 return output .value .decode ('utf-8' )
119121
120122
123+ def sha1 (data ):
124+ 'sha1 hash of data'
125+ output = ctypes .create_string_buffer (20 )
126+ poutput = ctypes .c_void_p (ctypes .addressof (output ))
127+ plugin .hashdata (data , len (data ), poutput )
128+ return output .raw [:20 ]
129+
130+
131+ def sha1hex (data ):
132+ 'sha1 hash of data'
133+ return plugin .hashdatahex (data , len (data )).decode ('utf-8' )
134+
135+
121136# pylint: disable=pointless-string-statement
122137'''
123138mykey = b'x123456789abcdefy123456789abcdefz123456789abcdef'
@@ -135,7 +150,6 @@ plugin.decryptrefname(('refs/heads/' + encoded).encode('utf-8'), poutput)
135150print(output.value)
136151plugin.decryptrefname(encoded.encode('utf-8'), poutput)
137152print(output.value)
138-
139153sys.exit(0)
140154'''
141155
@@ -147,8 +161,7 @@ class CryptRepo:
147161 def __init__ (self , clearname , url , init = None , forcetrust = False ):
148162 assert clearname , 'This does not work yet outside a git repository'
149163 plugin .globalinit ()
150- hashstr = hashlib .sha1 (url .encode ('utf-8' )).hexdigest ()
151- self .prefix = f'refs/incrypt/{ hashstr } /'
164+ self .prefix = f'refs/incrypt/{ sha1hex (url .encode ("utf-8" ))} /'
152165 self .url = url
153166 if init :
154167 self .repo = pygit2 .init_repository (clearname , bare = True )
@@ -479,18 +492,18 @@ class MetaData:
479492 self .files ['ver' ] = self .repo .create_blob (MetaData .VER )
480493 self .key = os .urandom (48 )
481494 keyhashbase = MetaData .KEYVER + b'\x00 ' + self .key
482- self .keyhash = hashlib . sha1 (keyhashbase ). hexdigest ( )
495+ self .keyhash = sha1hex (keyhashbase )
483496 cryptedkey = self ._gpg (
484497 ['-q' , '-e' ] + ['-r' + k for k in gpgkeys ], keyhashbase )
485498 self .files ['key' ] = self .repo .create_blob (cryptedkey )
486499 self .files ['sig' ] = self .repo .TreeBuilder ().write ()
487500 self .template = template
488501 self .files ['msg' ] = self .repo .create_blob (encryptdata (
489- hashlib . sha1 (template ). digest ( ) + template , self .key ))
502+ sha1 (template ) + template , self .key ))
490503 self .defaultbranch = defaultbranch
491504 encodedbranch = defaultbranch .encode ('utf-8' )
492505 self .files ['def' ] = self .repo .create_blob (encryptdata (
493- hashlib . sha1 (encodedbranch ). digest ( ) + encodedbranch , self .key ))
506+ sha1 (encodedbranch ) + encodedbranch , self .key ))
494507 self .write ()
495508 return self
496509
@@ -514,7 +527,7 @@ class MetaData:
514527 obj = tree ['key' ]
515528 self .files ['key' ] = obj .id
516529 data = self ._gpg (['-q' , '-d' ], obj .read_raw ())
517- newkeyhash = hashlib . sha1 (data ). hexdigest ( )
530+ newkeyhash = sha1hex (data )
518531 if self .keyhash :
519532 assert newkeyhash == self .keyhash , \
520533 f'Key hash is { newkeyhash } , was { self .keyhash } '
@@ -532,22 +545,21 @@ class MetaData:
532545 obj = tree ['msg' ]
533546 self .files ['msg' ] = obj .id
534547 data = decryptdata (obj .read_raw (), self .key )
535- assert hashlib .sha1 (data [20 :]).digest () == \
536- data [0 :20 ], 'corrupted template'
548+ assert sha1 (data [20 :]) == data [0 :20 ], 'corrupted template'
537549 self .template = data [20 :]
538550 obj = tree ['def' ]
539551 self .files ['def' ] = obj .id
540552 data = decryptdata (obj .read_raw (), self .key )
541- assert hashlib . sha1 (data [20 :]). digest () == \
542- data [ 0 : 20 ], 'corrupted default branch information'
553+ assert sha1 (data [20 :]) == data [ 0 : 20 ], \
554+ 'corrupted default branch information'
543555 self .defaultbranch = data [20 :].decode ('utf-8' )
544556 return self
545557
546558 def sign (self ):
547559 'sign key'
548560 sig = self ._gpg (['-q' , '-b' , '-s' ], self .key )
549561 sigfile = self .repo .create_blob (encryptdata (
550- hashlib . sha1 (sig ). digest ( ) + sig , self .key ))
562+ sha1 (sig ) + sig , self .key ))
551563 sigtree = self .repo .TreeBuilder (self .repo .get (self .files ['sig' ]))
552564 sigtree .insert (str (sigfile ), sigfile , pygit2 .enums .FileMode .BLOB )
553565 self .files ['sig' ] = sigtree .write ()
@@ -557,8 +569,8 @@ class MetaData:
557569 trusted = False
558570 for sig in self .repo .get (self .files ['sig' ]):
559571 decrypted = decryptdata (sig .read_raw (), self .key )
560- assert hashlib . sha1 (decrypted [20 :]). digest () == \
561- decrypted [ 0 : 20 ], 'corrupted signature'
572+ assert sha1 (decrypted [20 :]) == decrypted [ 0 : 20 ], \
573+ 'corrupted signature'
562574 with tempfile .NamedTemporaryFile (delete = False ) as tmp :
563575 tmp .write (decrypted [20 :])
564576 tmp .flush ()
@@ -596,7 +608,7 @@ class MetaData:
596608 for a , b in cryptmap .items ():
597609 rawdata += bytes .fromhex (a ) + bytes .fromhex (b )
598610 mapfile = self .repo .create_blob (encryptdata (
599- hashlib . sha1 (rawdata ). digest ( ) + rawdata , self .key ))
611+ sha1 (rawdata ) + rawdata , self .key ))
600612 collector .insert ('map' , mapfile , pygit2 .enums .FileMode .BLOB )
601613 readmefile = self .repo .create_blob (CRYPTREADME .encode ('utf-8' ))
602614 collector .insert ('README.md' , readmefile , pygit2 .enums .FileMode .BLOB )
0 commit comments