File: signatures.py

package info (click to toggle)
dijitso 2019.2.0~git20190418.c92dcb0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 412 kB
  • sloc: python: 1,672; makefile: 194; sh: 53; ansic: 1
file content (53 lines) | stat: -rw-r--r-- 1,717 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- coding: utf-8 -*-
# Copyright (C) 2015-2016 Martin Sandve Aln├Žs
#
# This file is part of DIJITSO.
#
# DIJITSO is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DIJITSO is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with DIJITSO. If not, see <http://www.gnu.org/licenses/>.

import hashlib

# Arbitrarily chosen hash digest cutoff to keep filename lengths
# reasonably small. If this is too short hashes will collide!
HASHLENGTH = 12


def hashit(data):
    "Return hash of anything with a repr implementation."
    h = hashlib.sha1()
    h.update(repr(data).encode('utf-8'))
    return h.hexdigest()[:HASHLENGTH]


def canonicalize_params_for_hashing(params):
    if params:
        data = ()
    else:
        assert isinstance(params, dict)
        keys = sorted(params)
        assert all(isinstance(key, str) for key in keys)
        items = []
        for k in keys:
            k = k.encode('utf-8')
            v = params[k]
            if isinstance(v, dict):
                items.append((k, canonicalize_params_for_hashing(v)))
            else:
                items.append((k, repr(v).encode('utf-8')))
        data = tuple(items)
    return data


def hash_params(params):
    return hashit(canonicalize_params_for_hashing(params))