File: localkey.py

package info (click to toggle)
python-pysnmp4 7.1.21-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,564 kB
  • sloc: python: 33,654; makefile: 166; javascript: 4
file content (79 lines) | stat: -rw-r--r-- 2,696 bytes parent folder | download
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#
# This file is part of pysnmp software.
#
# Copyright (c) 2005-2020, Ilya Etingof <etingof@gmail.com>
# License: https://www.pysnmp.com/pysnmp/license.html
#
from hashlib import md5, sha1

from pyasn1.type import univ


def hash_passphrase(passphrase, hashFunc) -> univ.OctetString:
    """Return hash of passphrase using hashFunc hash function."""
    passphrase = univ.OctetString(passphrase).asOctets()
    # noinspection PyDeprecation,PyCallingNonCallable
    hasher = hashFunc()
    ringBuffer = passphrase * (64 // len(passphrase) + 1)
    # noinspection PyTypeChecker
    ringBufferLen = len(ringBuffer)
    count = 0
    mark = 0
    while count < 16384:
        e = mark + 64
        if e < ringBufferLen:
            hasher.update(ringBuffer[mark:e])
            mark = e
        else:
            hasher.update(
                ringBuffer[mark:ringBufferLen] + ringBuffer[0 : e - ringBufferLen]
            )
            mark = e - ringBufferLen
        count += 1
    digest = hasher.digest()
    return univ.OctetString(digest)


def password_to_key(passphrase, snmpEngineId, hashFunc) -> univ.OctetString:
    """Return key from password."""
    return localize_key(hash_passphrase(passphrase, hashFunc), snmpEngineId, hashFunc)


def localize_key(passKey, snmpEngineId, hashFunc) -> univ.OctetString:
    """Localize passKey with snmpEngineId using hashFunc hash function."""
    passKey = univ.OctetString(passKey).asOctets()
    # noinspection PyDeprecation,PyCallingNonCallable
    digest = hashFunc(passKey + snmpEngineId.asOctets() + passKey).digest()
    return univ.OctetString(digest)


# RFC3414: A.2.1
def hash_passphrase_md5(passphrase) -> univ.OctetString:
    """Return MD5 hash of passphrase."""
    return hash_passphrase(passphrase, md5)


# RFC3414: A.2.2
def hash_passphrase_sha(passphrase) -> univ.OctetString:
    """Return SHA-1 hash of passphrase."""
    return hash_passphrase(passphrase, sha1)


def password_to_key_md5(passphrase, snmpEngineId) -> univ.OctetString:
    """Return MD5 key from password."""
    return localize_key(hash_passphrase_md5(passphrase), snmpEngineId, md5)


def password_to_key_sha(passphrase, snmpEngineId) -> univ.OctetString:
    """Return SHA-1 key from password."""
    return localize_key(hash_passphrase_sha(passphrase), snmpEngineId, sha1)


def localize_key_md5(passKey, snmpEngineId) -> univ.OctetString:
    """Localize passKey with snmpEngineId using MD5 hash function."""
    return localize_key(passKey, snmpEngineId, md5)


def localize_key_sha(passKey, snmpEngineId) -> univ.OctetString:
    """Localize passKey with snmpEngineId using SHA1 hash function."""
    return localize_key(passKey, snmpEngineId, sha1)