Last active
March 25, 2021 10:22
-
-
Save leeyisoft/80b9bdaccd3b2f93c879530de10993fd to your computer and use it in GitHub Desktop.
支持SHA-1/MD5消息摘要的工具类. Java 版本 和对应的PHP版本 和对应Python3版本 (在JDK 1.8.0_51 和 PHP 7.1.11 和 Python 3.6.0 测试ok);添加 pbkdf2_sha256 加密解密功能(在JAVA和Python对接成功)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright (c) 2005-2012 springside.org.cn | |
*/ | |
package com.rd.ifaes.common.util; | |
import com.rd.ifaes.common.exception.BussinessException; | |
import org.apache.commons.lang3.Validate; | |
import javax.crypto.SecretKeyFactory; | |
import javax.crypto.spec.PBEKeySpec; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.security.GeneralSecurityException; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.SecureRandom; | |
import java.security.spec.InvalidKeySpecException; | |
import java.util.Base64; | |
/** | |
* 支持SHA-1/MD5消息摘要的工具类. | |
* <p> | |
* 返回ByteSource,可进一步被编码为Hex, Base64或UrlSafeBase64 | |
* | |
* @author calvin | |
*/ | |
public class DigestUtils { | |
public static final String SHA1 = "SHA-1"; | |
public static final String MD5 = "md5"; | |
public static final String PBKDF2_SHA256 = "pbkdf2_sha256"; | |
private static final char[] chars = "0123456789abcdefghijklmnopqrwtuvzxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); | |
private static SecureRandom random = new SecureRandom(); | |
/** | |
* 对输入字符串进行md5散列. | |
*/ | |
public static byte[] md5(byte[] input) { | |
return digest(input, MD5, null, 1); | |
} | |
public static byte[] md5(byte[] input, int iterations) { | |
return digest(input, MD5, null, iterations); | |
} | |
public static byte[] md5(byte[] input, byte[] salt, int iterations) { | |
return digest(input, MD5, salt, iterations); | |
} | |
/** | |
* 对输入字符串进行sha1散列. | |
*/ | |
public static byte[] sha1(byte[] input) { | |
return digest(input, SHA1, null, 1); | |
} | |
public static byte[] sha1(byte[] input, byte[] salt) { | |
return digest(input, SHA1, salt, 1); | |
} | |
public static byte[] sha1(byte[] input, byte[] salt, int iterations) { | |
return digest(input, SHA1, salt, iterations); | |
} | |
/** | |
* 对字符串进行散列, 支持md5与sha1算法. | |
*/ | |
private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) { | |
try { | |
MessageDigest digest = MessageDigest.getInstance(algorithm); | |
if (salt != null) { | |
digest.update(salt); | |
} | |
byte[] result = digest.digest(input); | |
for (int i = 1; i < iterations; i++) { | |
digest.reset(); | |
result = digest.digest(result); | |
} | |
return result; | |
} catch (GeneralSecurityException e) { | |
throw new BussinessException(e.getMessage()); | |
} | |
} | |
/** | |
* 生成随机的Byte[]作为salt. | |
* | |
* @param numBytes byte数组的大小 | |
*/ | |
public static byte[] generateSalt(int numBytes) { | |
Validate.isTrue(numBytes > 0, "numBytes argument must be a positive integer (1 or larger)", numBytes); | |
byte[] saltchars = new byte[numBytes]; | |
for(int i=0; i<numBytes; i++) { | |
int n = random.nextInt(62); | |
saltchars[i]= (byte) chars[n]; | |
} | |
return saltchars; | |
} | |
/** | |
* 对文件进行md5散列. | |
*/ | |
public static byte[] md5(InputStream input) throws IOException { | |
return digest(input, MD5); | |
} | |
/** | |
* 对文件进行sha1散列. | |
*/ | |
public static byte[] sha1(InputStream input) throws IOException { | |
return digest(input, SHA1); | |
} | |
private static byte[] digest(InputStream input, String algorithm) throws IOException { | |
try { | |
MessageDigest messageDigest = MessageDigest.getInstance(algorithm); | |
int bufferLength = 8 * 1024; | |
byte[] buffer = new byte[bufferLength]; | |
int read = input.read(buffer, 0, bufferLength); | |
while (read > -1) { | |
messageDigest.update(buffer, 0, read); | |
read = input.read(buffer, 0, bufferLength); | |
} | |
return messageDigest.digest(); | |
} catch (GeneralSecurityException e) { | |
throw new BussinessException(e.getMessage()); | |
} | |
} | |
/** | |
* 对输入的密码进行验证 | |
* | |
* @param ciphertext | |
* 待验证的密码明文 | |
* @param encoded | |
* 密文 | |
* @return 是否验证成功 | |
* @throws NoSuchAlgorithmException | |
* @throws InvalidKeySpecException | |
*/ | |
public static boolean pbkdf2_sha256_verify(String ciphertext, String encoded) { | |
try { | |
// algorithm, iterations, salt, hash = encoded.split('$', 3) | |
String[] tmp = encoded.split("\\$"); | |
int iterations = Integer.parseInt(tmp[1]); | |
String salt = tmp[2]; | |
// 用相同的盐值对用户输入的密码进行加密 | |
String password = pbkdf2_sha256(ciphertext, salt.getBytes(), iterations); | |
// 把加密后的密文和原密文进行比较,相同则验证成功,否则失败 | |
return password.equals(encoded); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return false; | |
} | |
/** | |
* PBKDF2加密 | |
* @param ciphertext | |
* @return | |
*/ | |
public static String pbkdf2_sha256(String ciphertext, byte[] salt, int iterations) { | |
try { | |
int HASH_BIT_SIZE = 256; | |
char[] chars = ciphertext.toCharArray(); | |
final Base64.Encoder encoder = Base64.getEncoder(); | |
PBEKeySpec spec = new PBEKeySpec(chars, salt, iterations, HASH_BIT_SIZE); | |
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); | |
byte[] hash = skf.generateSecret(spec).getEncoded(); | |
// "%s$%d$%s$%s" % (algorithm, iterations, salt, hash) | |
return PBKDF2_SHA256 + "$"+ iterations + "$" + new String(salt) + "$" + encoder.encodeToString(hash); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace util; | |
/** | |
* 支持SHA-1/MD5消息摘要的工具类. | |
* | |
* 返回ByteSource,可进一步被编码为Hex, Base64或UrlSafeBase64 | |
* | |
* @author leeyi <leeyisoft@icloud.com> | |
*/ | |
class DigestUtils | |
{ | |
public $SHA1 = "sha1"; | |
public $MD5 = "md5"; | |
/** | |
* 对输入字符串进行md5散列. | |
*/ | |
public function md5($input, $salt = null, $iterations = 1) | |
{ | |
return $this->digest($input, $this->MD5, $salt, $iterations); | |
} | |
/** | |
* 对输入字符串进行sha1散列. | |
*/ | |
public function sha1($input, $salt = null, $iterations = 1) | |
{ | |
return $this->digest($input, $this->SHA1, $salt, $iterations); | |
} | |
/** | |
* 对字符串进行散列, 支持md5与sha1算法. | |
*/ | |
private function digest($input, $algorithm = 'MD5', $salt = null, $iterations = 1) | |
{ | |
try { | |
if (!empty($salt)) { | |
$result = $algorithm(hex2bin($salt) . $input); | |
} else { | |
$result = $algorithm($input); | |
} | |
for ($i = 1; $i < $iterations; ++$i) { | |
$result = $algorithm(hex2bin($result)); | |
} | |
return $result; | |
} catch (Exception $e) { | |
$e->getMessage(); | |
return false; | |
} | |
} | |
} | |
$digest = new DigestUtils(); | |
$input = 'abcd'; | |
// $salt = null; | |
// echo $digest->md5($input, $salt, 1); | |
// echo "\n"; | |
// echo $digest->md5($input, $salt, 2); | |
// echo "\n"; | |
// echo $digest->md5($input, $salt, 3); | |
// echo "\n"; | |
// echo $digest->md5($input, $salt, 1023); | |
// echo "\n"; | |
// echo $digest->md5($input, $salt, 1024); | |
// echo "\n"; | |
// echo "\n"; | |
// $salt = '68885a4704e5e3cb'; | |
// echo $digest->md5($input, $salt, 1); | |
// echo "\n"; | |
// echo $digest->md5($input, $salt, 2); | |
// echo "\n"; | |
// echo $digest->md5($input, $salt, 3); | |
// echo "\n"; | |
// echo $digest->md5($input, $salt, 1023); | |
// echo "\n"; | |
// echo $digest->md5($input, $salt, 1024); | |
// echo "\n"; | |
// echo "\n"; | |
$input = '783e41a659c37aac350d63282060f9ed'; | |
echo $digest->md5(hex2bin($input), $salt = null, 1023); | |
echo "\n"; | |
echo "\n"; | |
// echo "digest->sha1\n"; | |
// $salt = null; | |
// echo $digest->sha1($input, $salt, 1); | |
// echo "\n"; | |
// echo $digest->sha1($input, $salt, 2); | |
// echo "\n"; | |
// echo $digest->sha1($input, $salt, 3); | |
// echo "\n"; | |
// echo $digest->sha1($input, $salt, 1023); | |
// echo "\n"; | |
// echo $digest->sha1($input, $salt, 1024); | |
// echo "\n"; | |
// echo "\n"; | |
// $salt = '68885a4704e5e3cb'; | |
// echo $digest->sha1($input, $salt, 1); | |
// echo "\n"; | |
// echo $digest->sha1($input, $salt, 2); | |
// echo "\n"; | |
// echo $digest->sha1($input, $salt, 3); | |
// echo "\n"; | |
// echo $digest->sha1($input, $salt, 1023); | |
// echo "\n"; | |
// echo $digest->sha1($input, $salt, 1024); | |
// echo "\n"; | |
// echo "\n"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# encoding: utf-8 | |
import sys | |
import hashlib | |
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
"""URL处理器 | |
[description] | |
""" | |
import functools | |
import hashlib | |
import base64 | |
import hmac | |
import random | |
import datetime | |
from decimal import Decimal | |
from tornado.util import import_object | |
from applications.core.settings_manager import settings | |
UNUSABLE_PASSWORD_PREFIX = '!' # This will never be a valid encoded hash | |
UNUSABLE_PASSWORD_SUFFIX_LENGTH = 40 # number of random chars to add after UNUSABLE_PASSWORD_PREFIX | |
""" import ohter file --------------------""" | |
_PROTECTED_TYPES = ( | |
type(None), int, float, Decimal, datetime.datetime, datetime.date, datetime.time, | |
) | |
def is_protected_type(obj): | |
"""Determine if the object instance is of a protected type. | |
Objects of protected types are preserved as-is when passed to | |
force_text(strings_only=True). | |
""" | |
return isinstance(obj, _PROTECTED_TYPES) | |
def force_bytes(s, encoding='utf-8', strings_only=False, errors='strict'): | |
""" | |
Similar to smart_bytes, except that lazy instances are resolved to | |
strings, rather than kept as lazy objects. | |
If strings_only is True, don't convert (some) non-string-like objects. | |
""" | |
# Handle the common case first for performance reasons. | |
if isinstance(s, bytes): | |
if encoding == 'utf-8': | |
return s | |
else: | |
return s.decode('utf-8', errors).encode(encoding, errors) | |
if strings_only and is_protected_type(s): | |
return s | |
if isinstance(s, memoryview): | |
return bytes(s) | |
if isinstance(s, object) or not isinstance(s, str): | |
return str(s).encode(encoding, errors) | |
else: | |
return s.encode(encoding, errors) | |
def get_random_string(length=12, | |
allowed_chars='abcdefghijklmnopqrstuvwxyz' | |
'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'): | |
""" | |
Return a securely generated random string. | |
The default length of 12 with the a-z, A-Z, 0-9 character set returns | |
a 71-bit value. log_2((26+26+10)^12) =~ 71 bits | |
""" | |
return ''.join(random.choice(allowed_chars) for i in range(length)) | |
def pbkdf2(password, salt, iterations, dklen=0, digest=None): | |
"""Return the hash of password using pbkdf2.""" | |
if digest is None: | |
digest = hashlib.sha256 | |
if not dklen: | |
dklen = None | |
password = force_bytes(password) | |
salt = force_bytes(salt) | |
return hashlib.pbkdf2_hmac(digest().name, password, salt, iterations, dklen) | |
def constant_time_compare(val1, val2): | |
"""Return True if the two strings are equal, False otherwise.""" | |
return hmac.compare_digest(force_bytes(val1), force_bytes(val2)) | |
"""import ohter file end--------------------""" | |
def is_password_usable(encoded): | |
if encoded is None or encoded.startswith(UNUSABLE_PASSWORD_PREFIX): | |
return False | |
try: | |
identify_hasher(encoded) | |
except ValueError: | |
return False | |
return True | |
def check_password(password, encoded, setter=None, preferred='default'): | |
""" | |
Return a boolean of whether the raw password matches the three | |
part encoded digest. | |
If setter is specified, it'll be called when you need to | |
regenerate the password. | |
""" | |
if password is None or not is_password_usable(encoded): | |
return False | |
preferred = get_hasher(preferred) | |
hasher = identify_hasher(encoded) | |
hasher_changed = hasher.algorithm != preferred.algorithm | |
must_update = hasher_changed or preferred.must_update(encoded) | |
is_correct = hasher.verify(password, encoded) | |
# If the hasher didn't change (we don't protect against enumeration if it | |
# does) and the password should get updated, try to close the timing gap | |
# between the work factor of the current encoded password and the default | |
# work factor. | |
if not is_correct and not hasher_changed and must_update: | |
hasher.harden_runtime(password, encoded) | |
if setter and is_correct and must_update: | |
setter(password) | |
return is_correct | |
def make_password(password, salt=None, hasher='default'): | |
""" | |
Turn a plain-text password into a hash for database storage | |
Same as encode() but generate a new random salt. If password is None then | |
return a concatenation of UNUSABLE_PASSWORD_PREFIX and a random string, | |
which disallows logins. Additional random string reduces chances of gaining | |
access to staff or superuser accounts. See ticket #20079 for more info. | |
""" | |
if password is None: | |
return UNUSABLE_PASSWORD_PREFIX + get_random_string(UNUSABLE_PASSWORD_SUFFIX_LENGTH) | |
hasher = get_hasher(hasher) | |
if not salt: | |
salt = hasher.salt() | |
return hasher.encode(password, salt) | |
@functools.lru_cache() | |
def get_hashers(): | |
hashers = [] | |
for hasher_path in settings.PASSWORD_HASHERS: | |
hasher_cls = import_object(hasher_path) | |
hasher = hasher_cls() | |
if not getattr(hasher, 'algorithm'): | |
raise ImproperlyConfigured("hasher doesn't specify an " | |
"algorithm name: %s" % hasher_path) | |
hashers.append(hasher) | |
return hashers | |
@functools.lru_cache() | |
def get_hashers_by_algorithm(): | |
return {hasher.algorithm: hasher for hasher in get_hashers()} | |
def get_hasher(algorithm='default'): | |
""" | |
Return an instance of a loaded password hasher. | |
If algorithm is 'default', return the default hasher. Lazily import hashers | |
specified in the project's settings file if needed. | |
""" | |
if hasattr(algorithm, 'algorithm'): | |
return algorithm | |
elif algorithm == 'default': | |
return get_hashers()[0] | |
else: | |
hashers = get_hashers_by_algorithm() | |
try: | |
return hashers[algorithm] | |
except KeyError: | |
raise ValueError("Unknown password hashing algorithm '%s'. " | |
"Did you specify it in the PASSWORD_HASHERS " | |
"setting?" % algorithm) | |
def identify_hasher(encoded): | |
""" | |
Return an instance of a loaded password hasher. | |
Identify hasher algorithm by examining encoded hash, and call | |
get_hasher() to return hasher. Raise ValueError if | |
algorithm cannot be identified, or if hasher is not loaded. | |
""" | |
# Ancient versions of Django created plain MD5 passwords and accepted | |
# MD5 passwords with an empty salt. | |
if ((len(encoded) == 32 and '$' not in encoded) or | |
(len(encoded) == 37 and encoded.startswith('md5$$'))): | |
algorithm = 'unsalted_md5' | |
# Ancient versions of Django accepted SHA1 passwords with an empty salt. | |
elif len(encoded) == 46 and encoded.startswith('sha1$$'): | |
algorithm = 'unsalted_sha1' | |
else: | |
algorithm = encoded.split('$', 1)[0] | |
return get_hasher(algorithm) | |
def mask_hash(hash, show=6, char="*"): | |
""" | |
Return the given hash, with only the first ``show`` number shown. The | |
rest are masked with ``char`` for security reasons. | |
""" | |
masked = hash[:show] | |
masked += char * len(hash[show:]) | |
return masked | |
class BasePasswordHasher: | |
""" | |
Abstract base class for password hashers | |
When creating your own hasher, you need to override algorithm, | |
verify(), encode() and safe_summary(). | |
PasswordHasher objects are immutable. | |
""" | |
algorithm = None | |
library = None | |
def _load_library(self): | |
if self.library is not None: | |
if isinstance(self.library, (tuple, list)): | |
name, mod_path = self.library | |
else: | |
mod_path = self.library | |
try: | |
module = importlib.import_module(mod_path) | |
except ImportError as e: | |
raise ValueError("Couldn't load %r algorithm library: %s" % | |
(self.__class__.__name__, e)) | |
return module | |
raise ValueError("Hasher %r doesn't specify a library attribute" % | |
self.__class__.__name__) | |
def salt(self): | |
"""Generate a cryptographically secure nonce salt in ASCII.""" | |
return get_random_string(16) | |
def verify(self, password, encoded): | |
"""Check if the given password is correct.""" | |
raise NotImplementedError('subclasses of BasePasswordHasher must provide a verify() method') | |
def encode(self, password, salt): | |
""" | |
Create an encoded database value. | |
The result is normally formatted as "algorithm$salt$hash" and | |
must be fewer than 128 characters. | |
""" | |
raise NotImplementedError('subclasses of BasePasswordHasher must provide an encode() method') | |
def safe_summary(self, encoded): | |
""" | |
Return a summary of safe values. | |
The result is a dictionary and will be used where the password field | |
must be displayed to construct a safe representation of the password. | |
""" | |
raise NotImplementedError('subclasses of BasePasswordHasher must provide a safe_summary() method') | |
def must_update(self, encoded): | |
return False | |
def harden_runtime(self, password, encoded): | |
""" | |
Bridge the runtime gap between the work factor supplied in `encoded` | |
and the work factor suggested by this hasher. | |
Taking PBKDF2 as an example, if `encoded` contains 20000 iterations and | |
`self.iterations` is 30000, this method should run password through | |
another 10000 iterations of PBKDF2. Similar approaches should exist | |
for any hasher that has a work factor. If not, this method should be | |
defined as a no-op to silence the warning. | |
""" | |
warnings.warn('subclasses of BasePasswordHasher should provide a harden_runtime() method') | |
class PBKDF2PasswordHasher(BasePasswordHasher): | |
""" | |
Secure password hashing using the PBKDF2 algorithm (recommended) | |
Configured to use PBKDF2 + HMAC + SHA256. | |
The result is a 64 byte binary string. Iterations may be changed | |
safely but you must rename the algorithm if you change SHA256. | |
""" | |
algorithm = "pbkdf2_sha256" | |
iterations = 100000 | |
digest = hashlib.sha256 | |
def encode(self, password, salt, iterations=None): | |
assert password is not None | |
assert salt and '$' not in salt | |
if not iterations: | |
iterations = self.iterations | |
hash = pbkdf2(password, salt, iterations, digest=self.digest) | |
hash = base64.b64encode(hash).decode('utf-8').strip() | |
return "%s$%d$%s$%s" % (self.algorithm, iterations, salt, hash) | |
def verify(self, password, encoded): | |
algorithm, iterations, salt, hash = encoded.split('$', 3) | |
assert algorithm == self.algorithm | |
encoded_2 = self.encode(password, salt, int(iterations)) | |
return constant_time_compare(encoded, encoded_2) | |
def safe_summary(self, encoded): | |
algorithm, iterations, salt, hash = encoded.split('$', 3) | |
assert algorithm == self.algorithm | |
return OrderedDict([ | |
(_('algorithm'), algorithm), | |
(_('iterations'), iterations), | |
(_('salt'), mask_hash(salt)), | |
(_('hash'), mask_hash(hash)), | |
]) | |
def must_update(self, encoded): | |
algorithm, iterations, salt, hash = encoded.split('$', 3) | |
return int(iterations) != self.iterations | |
def harden_runtime(self, password, encoded): | |
algorithm, iterations, salt, hash = encoded.split('$', 3) | |
extra_iterations = self.iterations - int(iterations) | |
if extra_iterations > 0: | |
self.encode(password, salt, extra_iterations) | |
class PBKDF2SHA1PasswordHasher(PBKDF2PasswordHasher): | |
""" | |
Alternate PBKDF2 hasher which uses SHA1, the default PRF | |
recommended by PKCS #5. This is compatible with other | |
implementations of PBKDF2, such as openssl's | |
PKCS5_PBKDF2_HMAC_SHA1(). | |
""" | |
algorithm = "pbkdf2_sha1" | |
digest = hashlib.sha1 | |
class DigestUtils(object): | |
SHA1 = "sha1"; | |
MD5 = "md5"; | |
def md5(self, input_str, salt = None, iterations = 1): | |
""" | |
""" | |
return self._digest(input_str, self.MD5, salt, iterations) | |
def sha1(self, input_str, salt = None, iterations = 1): | |
""" | |
""" | |
return self._digest(input_str, self.SHA1, salt, iterations) | |
def _digest(self, input_str, algorithm, salt, iterations): | |
"""[summary] | |
支持SHA-1/MD5消息摘要的工具类. | |
返回ByteSource,可进一步被编码为Hex, Base64或UrlSafeBase64 | |
@author leeyi <leeyisoft@icloud.com> | |
Arguments: | |
input_str {[type]} -- [description] | |
algorithm {[type]} -- [description] | |
salt {[type]} -- [description] | |
iterations {[type]} -- [description] | |
Returns: | |
[hex string] -- [16进制字符串] | |
""" | |
hash = hashlib.md5() if algorithm==self.MD5 else hashlib.sha1() | |
if salt is not None : | |
hash.update(bytes().fromhex(salt)) | |
hash.update(input_str.encode('utf-8')) | |
result = hash.digest() | |
if iterations>1: | |
counter = 1 | |
while counter < iterations: | |
hash = hashlib.md5() if algorithm==self.MD5 else hashlib.sha1() | |
hash.update(result) | |
result = hash.digest() | |
counter += 1 | |
return result.hex() | |
if __name__ == '__main__': | |
try: | |
digest = DigestUtils() | |
input_str = "abcd" | |
salt = None | |
print('iterations 1: ', digest.md5(input_str, salt, 1)=='e2fc714c4727ee9395f324cd2e7f331f') | |
print('iterations 3: ', digest.md5(input_str, salt, 3)=='b053351d1e3225dace70e93819287fab') | |
print('iterations 1024: ', digest.md5(input_str, salt, 1024)=='af04fc42bee897b183ec6336904c2bfa') | |
print("==============salt============") | |
salt = '68885a4704e5e3cb' | |
res1 = digest.md5(input_str, salt, 1) | |
print('iterations 1: ', res1=='97cac52a62898aa5d5913f8e945c59ef', res1) | |
res3 = digest.md5(input_str, salt, 3) | |
print('iterations 3: ', res3=='bcdba702844ed91b6ed7dc85544d5125', res3) | |
res1024 = digest.md5(input_str, salt, 1024) | |
print('iterations 1024: ', res1024=='20abe1fbad8fc497fcfd0896e2079c85', res1024) | |
print("==============sha1 salt============") | |
salt = '68885a4704e5e3cb' | |
res1 = digest.sha1(input_str, salt, 1) | |
print('iterations 1: ', res1=='dedee6bf941bbb803d4c3ef8d216a96bd7051864', res1) | |
res3 = digest.sha1(input_str, salt, 3) | |
print('iterations 3: ', res3=='50ef7d0535ef1651e7edc6d93d33d12060be2013', res3) | |
res1024 = digest.sha1(input_str, salt, 1024) | |
print('iterations 1024: ', res1024=='9c3b42eac729e8b4f1e694058abfd8bb7ca7e40f', res1024) | |
except KeyboardInterrupt: | |
sys.exit(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
php | |
salt = null | |
e2fc714c4727ee9395f324cd2e7f331f | |
1243e2c5302cae4b559ce80dd1cefa6e | |
b053351d1e3225dace70e93819287fab | |
81e911500587ff264bb558564edc97cf | |
af04fc42bee897b183ec6336904c2bfa | |
97cac52a62898aa5d5913f8e945c59ef | |
a69fab77365f678ed73b9405cc07eac3 | |
bcdba702844ed91b6ed7dc85544d5125 | |
bae5973925d356abb158f00df8cba74e | |
20abe1fbad8fc497fcfd0896e2079c85 | |
java | |
salt = null | |
e2fc714c4727ee9395f324cd2e7f331f | |
1243e2c5302cae4b559ce80dd1cefa6e | |
b053351d1e3225dace70e93819287fab | |
81e911500587ff264bb558564edc97cf | |
af04fc42bee897b183ec6336904c2bfa | |
97cac52a62898aa5d5913f8e945c59ef | |
a69fab77365f678ed73b9405cc07eac3 | |
bcdba702844ed91b6ed7dc85544d5125 | |
bae5973925d356abb158f00df8cba74e | |
20abe1fbad8fc497fcfd0896e2079c85 | |
digest->sha1 | |
php | |
$salt = null; | |
81fe8bfe87576c3ecb22426f8e57847382917acf | |
a154c52565e9e7f94bfc08a1fe702624ed8effda | |
4dd3b406c37c7c0fa9785e820ba4d1ee52958c07 | |
8cf124f378271488fb75a3efb93c4d08729319a4 | |
0913ad8efd378f57b2bb0e9522484c6c020b74d6 | |
dedee6bf941bbb803d4c3ef8d216a96bd7051864 | |
6cb5ea32bc7aa8fc050439ab490e6e4eef3b149b | |
50ef7d0535ef1651e7edc6d93d33d12060be2013 | |
749032ef3609c89fe1b265f34dbc8b7ef2cef507 | |
9c3b42eac729e8b4f1e694058abfd8bb7ca7e40f | |
java | |
81fe8bfe87576c3ecb22426f8e57847382917acf | |
a154c52565e9e7f94bfc08a1fe702624ed8effda | |
4dd3b406c37c7c0fa9785e820ba4d1ee52958c07 | |
8cf124f378271488fb75a3efb93c4d08729319a4 | |
0913ad8efd378f57b2bb0e9522484c6c020b74d6 | |
dedee6bf941bbb803d4c3ef8d216a96bd7051864 | |
6cb5ea32bc7aa8fc050439ab490e6e4eef3b149b | |
50ef7d0535ef1651e7edc6d93d33d12060be2013 | |
749032ef3609c89fe1b265f34dbc8b7ef2cef507 | |
9c3b42eac729e8b4f1e694058abfd8bb7ca7e40f | |
python3 | |
salt = 'mOZ62TvXIQlNpNwa' | |
# salt = None | |
pwd1 = make_password('123456', salt, 'pbkdf2_sha256') | |
pwd2 = 'pbkdf2_sha256$100000$mOZ62TvXIQlNpNwM$pIb4Li+qOFTAu547UXarGP/K10Dix3b8WELt3iGVRCI=' | |
pwd3 = check_password('123456', pwd2) | |
script/py3 [master●] » python mv_user_data.py | |
iterations 1: True | |
iterations 3: True | |
iterations 1024: True | |
==============salt============ | |
iterations 1: True 97cac52a62898aa5d5913f8e945c59ef | |
iterations 3: True bcdba702844ed91b6ed7dc85544d5125 | |
iterations 1024: True 20abe1fbad8fc497fcfd0896e2079c85 | |
==============sha1 salt============ | |
iterations 1: True dedee6bf941bbb803d4c3ef8d216a96bd7051864 | |
iterations 3: True 50ef7d0535ef1651e7edc6d93d33d12060be2013 | |
iterations 1024: True 9c3b42eac729e8b4f1e694058abfd8bb7ca7e40f | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment