6
6
#
7
7
try :
8
8
from hashlib import md5 , sha1
9
+
9
10
except ImportError :
10
11
import md5
11
12
import sha
12
13
13
14
md5 = md5 .new
14
15
sha1 = sha .new
16
+
15
17
from pyasn1 .type import univ
16
18
17
19
@@ -37,30 +39,38 @@ def hashPassphrase(passphrase, hashFunc):
37
39
count += 1
38
40
return hasher .digest ()
39
41
42
+
40
43
def passwordToKey (passphrase , snmpEngineId , hashFunc ):
41
44
return localizeKey (hashPassphrase (passphrase , hashFunc ), snmpEngineId , hashFunc )
42
45
46
+
43
47
def localizeKey (passKey , snmpEngineId , hashFunc ):
44
48
passKey = univ .OctetString (passKey ).asOctets ()
45
49
# noinspection PyDeprecation,PyCallingNonCallable
46
50
return hashFunc (passKey + snmpEngineId .asOctets () + passKey ).digest ()
47
51
52
+
48
53
# RFC3414: A.2.1
49
54
def hashPassphraseMD5 (passphrase ):
50
55
return hashPassphrase (passphrase , md5 )
51
56
57
+
52
58
# RFC3414: A.2.2
53
59
def hashPassphraseSHA (passphrase ):
54
60
return hashPassphrase (passphrase , sha1 )
55
61
62
+
56
63
def passwordToKeyMD5 (passphrase , snmpEngineId ):
57
64
return localizeKey (hashPassphraseMD5 (passphrase ), snmpEngineId , md5 )
58
65
66
+
59
67
def passwordToKeySHA (passphrase , snmpEngineId ):
60
68
return localizeKey (hashPassphraseMD5 (passphrase ), snmpEngineId , sha1 )
61
69
70
+
62
71
def localizeKeyMD5 (passKey , snmpEngineId ):
63
72
return localizeKey (passKey , snmpEngineId , md5 )
64
73
74
+
65
75
def localizeKeySHA (passKey , snmpEngineId ):
66
76
return localizeKey (passKey , snmpEngineId , sha1 )
0 commit comments