Created
January 16, 2020 08:34
-
-
Save Jabriko/b5e30dad097559c69fb89534181e7262 to your computer and use it in GitHub Desktop.
Hasil modifikasi JPasswordField ke MD5.. Menjadi JPasswordField ke MD5 + Salt.
This file contains hidden or 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
| /* | |
| * To change this license header, choose License Headers in Project Properties. | |
| * To change this template file, choose Tools | Templates | |
| * and open the template in the editor. | |
| */ | |
| package passwordmd5withsalt; | |
| import java.security.MessageDigest; | |
| import java.security.NoSuchAlgorithmException; | |
| import java.security.NoSuchProviderException; | |
| import java.security.SecureRandom; | |
| import java.util.ArrayList; | |
| /** | |
| * | |
| * @author indrasudirman | |
| */ | |
| public class JPasswordMD5Example2 { | |
| public static String digest (char [] in, byte[] salt) throws NoSuchAlgorithmException { | |
| MessageDigest messageDigest = MessageDigest.getInstance("MD5"); | |
| ArrayList<Byte> list = new ArrayList<Byte>(); | |
| for (int i = 0; i < in.length; i++) { | |
| byte b = (byte) in[i]; | |
| list.add(b); | |
| } | |
| byte [] inputInByte = new byte [list.size()]; | |
| for (int i = 0; i < list.size(); i++) { | |
| inputInByte[i] = list.get(i); | |
| } | |
| messageDigest.update(salt); | |
| byte byteData [] = messageDigest.digest(); | |
| StringBuffer hexString = new StringBuffer(); | |
| for (int i = 0; i < byteData.length; i++) { | |
| String hex = Integer.toHexString(0xff & byteData[i]); | |
| if (hex.length() == 1) { | |
| hexString.append('0'); | |
| } | |
| hexString.append(hex); | |
| } | |
| return hexString.toString(); | |
| } | |
| public static byte[] getSalt() throws NoSuchAlgorithmException, NoSuchProviderException { | |
| //Always use a SecureRandom generator | |
| SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG", "SUN"); | |
| //Create array for salt | |
| byte [] salt = new byte[16]; | |
| //Get a random salt | |
| secureRandom.nextBytes(salt); | |
| //return salt | |
| return salt; | |
| } | |
| public static void main (String [] args) throws NoSuchAlgorithmException, NoSuchProviderException { | |
| char[] pass = {'h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd'}; | |
| byte[] salt = getSalt(); | |
| String str2 = digest(pass, salt); | |
| System.out.println(str2); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 20 : Penambahan 1 parameter byte [] salt
Line 35: Menjadi = messageDigest.update(salt); Sebelumnya = messageDigest.update(inputInByte); inputInByte-nya jadi tidak terpakai om. Ini yang saya bingung. Apa, tetap berhasil MD5 + Salt-nya.
Line 52 - 62: Penambahan method getSalt(); sebelumnya di MD5 tidak ada.
Implementasinya:
Line 67: variabel salt byte[] = getSalt();
Line 68: menambah String str2 = digest(pass, salt); sebelumnya: String str2 = digest(pass); tidak pakai salt.