-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
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
24 changes: 24 additions & 0 deletions
24
core/src/main/java/me/mastercapexd/auth/crypto/belkaauth/UAuthCryptoProvider.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package me.mastercapexd.auth.crypto.belkaauth; | ||
|
||
import com.bivashy.auth.api.crypto.CryptoProvider; | ||
import com.bivashy.auth.api.crypto.HashInput; | ||
import com.bivashy.auth.api.crypto.HashedPassword; | ||
|
||
public class UAuthCryptoProvider implements CryptoProvider { | ||
private static final String SALT = "saharPidor"; | ||
|
||
@Override | ||
public HashedPassword hash(HashInput input) { | ||
return HashedPassword.of(UAuthUtil.getHash(SALT, input.getRawInput()), SALT, this); | ||
} | ||
|
||
@Override | ||
public boolean matches(HashInput input, HashedPassword password) { | ||
return hash(input).getHash().equals(password.getHash()); | ||
} | ||
|
||
@Override | ||
public String getIdentifier() { | ||
return "UAUTH_UNSAFE"; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
core/src/main/java/me/mastercapexd/auth/crypto/belkaauth/UAuthUtil.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package me.mastercapexd.auth.crypto.belkaauth; | ||
|
||
import java.security.NoSuchAlgorithmException; | ||
import java.security.SecureRandom; | ||
|
||
import javax.crypto.SecretKeyFactory; | ||
import javax.crypto.spec.PBEKeySpec; | ||
|
||
public final class UAuthUtil { | ||
private static SecureRandom RANDOM; | ||
|
||
public static String generateSalt() { | ||
byte[] salt = new byte[20]; | ||
RANDOM.nextBytes(salt); | ||
return new String(bytesToString(salt)); | ||
} | ||
|
||
public static byte[] getHash(String pass, byte[] salt, int iter, int len, String alg) throws Exception { | ||
PBEKeySpec ks = new PBEKeySpec(pass.toCharArray(), salt, iter, len); | ||
SecretKeyFactory skf = SecretKeyFactory.getInstance(alg); | ||
return skf.generateSecret(ks).getEncoded(); | ||
} | ||
|
||
public static String getHash(String pass, String salt, int iter, int len, String alg) throws Exception { | ||
byte[] hash = getHash(pass, stringToBytes(salt), iter, len, alg); | ||
return new String(bytesToString(hash)); | ||
} | ||
|
||
public static char[] bytesToString(byte[] b) { | ||
char[] res = new char[b.length * 2]; | ||
for (int i = 0; i < b.length; i++) { | ||
res[i * 2] = LOOKUP[b[i] >>> 4 & 0xF]; | ||
res[i * 2 + 1] = LOOKUP[b[i] & 0xF]; | ||
} | ||
return res; | ||
} | ||
|
||
public static byte[] stringToBytes(String s) { | ||
int len = s.length(); | ||
byte[] data = new byte[len / 2]; | ||
for (int i = 0; i < len; i += 2) | ||
data[i / 2] = (byte)((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16)); | ||
return data; | ||
} | ||
|
||
public static String getHash(String salt, String password) { | ||
try { | ||
return getHash(password + "1_jm6H3tbLZ3DwZAqy8kVxAmjEJhl8ASypKUP-3d", salt, 50000, 160, "PBKDF2WithHmacSHA256"); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
|
||
private UAuthUtil() { | ||
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); | ||
} | ||
|
||
static { | ||
try { | ||
RANDOM = SecureRandom.getInstance("SHA1PRNG"); | ||
} catch (NoSuchAlgorithmException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private static char[] LOOKUP = "0123456789abcdef".toCharArray(); | ||
} |