PasswordReciprocal optimize
This commit is contained in:
@@ -132,55 +132,18 @@ public final class ReciprocalUtils {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static byte[] encodeByDefaultKey(String simple, String algorithm) {
|
||||
SecretKey key = generatorDefaultKey(algorithm);
|
||||
return encode(simple.getBytes(), key, algorithm);
|
||||
|
||||
}
|
||||
|
||||
public static String encode2HexByDefaultKey(String simple, String algorithm) {
|
||||
byte[] byteFinal = encodeByDefaultKey(simple, algorithm);
|
||||
|
||||
String cipherHex = HexUtils.bytes2HexString(byteFinal);
|
||||
return cipherHex;
|
||||
}
|
||||
|
||||
public static byte[] decoderByDefaultKey(byte[] byteCiphers, String algorithm) {
|
||||
SecretKey key = generatorDefaultKey(algorithm);
|
||||
return decoder(byteCiphers, key, algorithm);
|
||||
|
||||
}
|
||||
|
||||
public static String decoderHexByDefaultKey(String ciphers, String algorithm) {
|
||||
if(StringUtils.isBlank(ciphers))return "";
|
||||
|
||||
byte[] byteSimple = HexUtils.hex2Bytes(ciphers);
|
||||
|
||||
byte[] byteFinal = decoderByDefaultKey(byteSimple, algorithm);
|
||||
|
||||
String simple = null;
|
||||
public static String generatorDefaultKey(String secretKey,String algorithm) {
|
||||
try {
|
||||
simple = new String(byteFinal, "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return simple;
|
||||
|
||||
}
|
||||
|
||||
public static SecretKey generatorDefaultKey(String algorithm) {
|
||||
try {
|
||||
String secretKey = defaultKey;
|
||||
secretKey = secretKey + defaultKey;
|
||||
if (algorithm.equals(Algorithm.DES)) {
|
||||
secretKey = defaultKey.substring(0, 8);
|
||||
secretKey = secretKey.substring(0, 8);
|
||||
} else if (algorithm.equals(Algorithm.AES) || algorithm.equals(Algorithm.Blowfish)) {
|
||||
secretKey = defaultKey.substring(0, 16);
|
||||
secretKey = secretKey.substring(0, 16);
|
||||
} else if (algorithm.equals(Algorithm.DESede)) {
|
||||
secretKey = defaultKey.substring(0, 24);
|
||||
secretKey = secretKey.substring(0, 24);
|
||||
}
|
||||
// System.out.println("defaultKey : "+secretKey);
|
||||
SecretKey key = new SecretKeySpec(secretKey.getBytes(), algorithm);
|
||||
return key;
|
||||
return secretKey;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -216,7 +179,17 @@ public final class ReciprocalUtils {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String encode2Hex(String simple, String secretKey) {
|
||||
String key = generatorDefaultKey(secretKey + defaultKey,Algorithm.DESede);
|
||||
return encode2Hex(simple,key, Algorithm.DESede);
|
||||
}
|
||||
|
||||
public static String decoderHex(String ciphers, String secretKey) {
|
||||
String key = generatorDefaultKey(secretKey + defaultKey,Algorithm.DESede);
|
||||
return decoderHex(ciphers,key,Algorithm.DESede);
|
||||
}
|
||||
|
||||
private static boolean keyLengthCheck(String secretKey, String algorithm) {
|
||||
boolean lengthCheck = false;
|
||||
if (algorithm.equals(Algorithm.DES)) {
|
||||
@@ -264,27 +237,6 @@ public final class ReciprocalUtils {
|
||||
return decoderHex(ciphers, secretKey, Algorithm.AES);
|
||||
}
|
||||
|
||||
/**
|
||||
* encode by defaultKey with Algorithm.AES
|
||||
*
|
||||
* @param simple
|
||||
* @return Hex
|
||||
*/
|
||||
public static String encode(String simple) {
|
||||
return encode2HexByDefaultKey(simple, Algorithm.AES);
|
||||
}
|
||||
|
||||
/**
|
||||
* decoder by defaultKey with Algorithm.AES
|
||||
*
|
||||
* @param ciphers is HEX
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String decoder(String ciphers) {
|
||||
return decoderHexByDefaultKey(ciphers, Algorithm.AES);
|
||||
}
|
||||
|
||||
public static String generateKey(String algorithm) {
|
||||
if (algorithm.equals(Algorithm.DES)) {
|
||||
return (new StringGenerator(8)).randomGenerate();
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
package org.maxkey.crypto.password;
|
||||
|
||||
import org.maxkey.crypto.ReciprocalUtils;
|
||||
import org.springframework.security.crypto.bcrypt.BCrypt;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
|
||||
/**
|
||||
@@ -27,8 +28,10 @@ import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
*/
|
||||
public class PasswordReciprocal implements PasswordEncoder {
|
||||
|
||||
public static int PREFFIX_LENGTH = 7;
|
||||
|
||||
public static PasswordReciprocal passwordReciprocal;
|
||||
|
||||
|
||||
public PasswordReciprocal() {
|
||||
|
||||
}
|
||||
@@ -45,24 +48,38 @@ public class PasswordReciprocal implements PasswordEncoder {
|
||||
|
||||
return passwordReciprocal;
|
||||
}
|
||||
|
||||
public String rawPassword(String username, String password) {
|
||||
return password + "@" + username;
|
||||
}
|
||||
|
||||
public String encode(CharSequence rawPassword) {
|
||||
return ReciprocalUtils.encode(rawPassword.toString());
|
||||
|
||||
public String decoder(CharSequence encodedPassword) {
|
||||
String salt = encodedPassword.subSequence(0, 29).toString();
|
||||
encodedPassword = encodedPassword.subSequence(29, encodedPassword.length());
|
||||
String plain = ReciprocalUtils.decoderHex(encodedPassword.toString(), salt.substring(PREFFIX_LENGTH));
|
||||
return plain.substring(salt.substring(PREFFIX_LENGTH).length());
|
||||
}
|
||||
|
||||
public boolean matches(CharSequence rawPassword, String encodedPassword) {
|
||||
return ReciprocalUtils.encode(rawPassword.toString()).equals(encodedPassword);
|
||||
String salt = encodedPassword.subSequence(0, 29).toString();
|
||||
String finalPassword = encode(rawPassword,salt);
|
||||
return finalPassword.equals(encodedPassword);//ReciprocalUtils.encode(rawPassword.toString()).equals(encodedPassword);
|
||||
}
|
||||
|
||||
public String decoder(CharSequence encodedPassword) {
|
||||
if(encodedPassword == null || encodedPassword.equals("")) {
|
||||
return "";
|
||||
}
|
||||
return ReciprocalUtils.decoder(encodedPassword.toString());
|
||||
/**
|
||||
* salt
|
||||
* length 29
|
||||
* @return salt
|
||||
*/
|
||||
public String gensalt() {
|
||||
return BCrypt.gensalt("$2a", 10);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String encode(CharSequence plain) {
|
||||
//$2a$10$
|
||||
String salt = gensalt();
|
||||
return encode(plain, salt);
|
||||
}
|
||||
|
||||
private String encode(CharSequence plain,String salt) {
|
||||
String password = salt.substring(PREFFIX_LENGTH) + plain ;
|
||||
return salt + ReciprocalUtils.encode2Hex(password , salt.substring(PREFFIX_LENGTH));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user