From a42c75e6fe07162b811778e66b974b730700b0de Mon Sep 17 00:00:00 2001
From: "Antoine R. Dumont (@ardumont)" <antoine.romain.dumont@gmail.com>
Date: Thu, 16 Mar 2017 18:10:18 +0100
Subject: [PATCH] swh.model.hashutil: Use pyblake2 dependency on python3 <= 3.4

This resolves the caveat mentioned in prior commit about not being
able to use blake2 prior to 3.5

Related T692
Closes D192
---
 debian/control        |  1 +
 requirements-swh.txt  |  1 +
 swh/model/hashutil.py | 10 ++++++++++
 3 files changed, 12 insertions(+)

diff --git a/debian/control b/debian/control
index c20e5f56..3e7fc44e 100644
--- a/debian/control
+++ b/debian/control
@@ -7,6 +7,7 @@ Build-Depends: debhelper (>= 9),
                python3-all,
                python3-nose,
                python3-setuptools,
+               python3-pyblake2,
                python3-vcversioner
 Standards-Version: 3.9.6
 Homepage: https://forge.softwareheritage.org/diffusion/DMOD/
diff --git a/requirements-swh.txt b/requirements-swh.txt
index e69de29b..fbe63bd9 100644
--- a/requirements-swh.txt
+++ b/requirements-swh.txt
@@ -0,0 +1 @@
+pyblake2
diff --git a/swh/model/hashutil.py b/swh/model/hashutil.py
index aa047869..20bde979 100644
--- a/swh/model/hashutil.py
+++ b/swh/model/hashutil.py
@@ -28,6 +28,7 @@ import binascii
 import functools
 import hashlib
 import os
+import sys
 
 from io import BytesIO
 
@@ -41,6 +42,15 @@ DEFAULT_ALGORITHMS = set(['sha1', 'sha256', 'sha1_git'])
 # FWIW coreutils' sha1sum uses 32768
 HASH_BLOCK_SIZE = 32768
 
+# Prior to python3.4, only blake2 is available through pyblake2 module
+# From 3.5 onwards, it's been integrated in python
+if sys.version_info.major == 3 and sys.version_info.minor <= 4:
+    import pyblake2
+    # register those hash algorithms in hashlib
+    __cache = hashlib.__builtin_constructor_cache
+    __cache['blake2s256'] = pyblake2.blake2s
+    __cache['blake2b512'] = pyblake2.blake2b
+
 
 def _new_git_hash(base_algo, git_type, length):
     """Initialize a digest object (as returned by python's hashlib) for the
-- 
GitLab