Created
February 22, 2015 17:38
-
-
Save is/13a519282e676dca861e to your computer and use it in GitHub Desktop.
PBKDF2 Python & Java
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
| import java.security.NoSuchAlgorithmException; | |
| import java.security.spec.InvalidKeySpecException; | |
| import java.security.spec.KeySpec; | |
| import javax.crypto.SecretKey; | |
| import javax.crypto.SecretKeyFactory; | |
| import javax.crypto.spec.PBEKeySpec; | |
| // Reference | |
| // Keyword: PBKDF2WithHmacSHA1 PBKDF2WithHmacSHA512 | |
| // http://stackoverflow.com/questions/19348501/pbkdf2withhmacsha512-vs-pbkdf2withhmacsha1 | |
| public class Aes { | |
| public static void main(String args[]) throws Exception { | |
| Aes app = new Aes(); | |
| app.run(); | |
| } | |
| void run() throws NoSuchAlgorithmException, InvalidKeySpecException { | |
| String initKey = "PYTHON&JAVA"; | |
| PBEKeySpec spec0 = new PBEKeySpec(initKey.toCharArray(), | |
| "1234567812345678".getBytes(), 65536, 128); | |
| SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); | |
| SecretKey spec1 = skf.generateSecret(spec0); | |
| System.out.println("PBKDF2WithHmacSHA1:" + Utils.bytesToHex(spec1.getEncoded())); | |
| spec0 = new PBEKeySpec(initKey.toCharArray(), | |
| "12345678".getBytes(), 65536, 128); | |
| spec1 = skf.generateSecret(spec0); | |
| System.out.println("PBKDF2WithHmacSHA1 - 8 bytes salt:" + Utils.bytesToHex(spec1.getEncoded())); | |
| spec0 = new PBEKeySpec(initKey.toCharArray(), | |
| "1234567812345678".getBytes(), 65536, 128); | |
| skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512"); | |
| spec1 = skf.generateSecret(spec0); | |
| System.out.println("PBKDF2WithHmacSHA512:" + Utils.bytesToHex(spec1.getEncoded())); | |
| spec0 = new PBEKeySpec(initKey.toCharArray(), | |
| "12345678".getBytes(), 65536, 128); | |
| spec1 = skf.generateSecret(spec0); | |
| System.out.println("PBKDF2WithHmacSHA512 - 8 bytes salt:" + Utils.bytesToHex(spec1.getEncoded())); | |
| } | |
| } |
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
| from passlib.hash import pbkdf2_sha1, pbkdf2_sha512 | |
| from passlib.utils import ab64_encode, ab64_decode | |
| import binascii | |
| def b64(s): | |
| size = len(s) | |
| padding = 4 - size % 4 | |
| if padding == 4: | |
| padding = 0 | |
| print(len(s)) | |
| sp = s + "=" * padding | |
| print(len(sp)) | |
| print(sp) | |
| return binascii.a2b_base64(sp) | |
| key0 = "PYTHON&JAVA" | |
| salt0 = "1234567812345678".encode("utf8") | |
| print(len(salt0)) | |
| hash = pbkdf2_sha1.encrypt(key0, salt = salt0, salt_size = 16, rounds = 65536) | |
| print("PBKDF2WithHmacSHA1:") | |
| print(hash) | |
| print(binascii.b2a_hex(binascii.a2b_base64(hash.split('$')[-1] + "=")).decode()) | |
| hash = pbkdf2_sha1.encrypt(key0, | |
| salt = "12345678".encode(), salt_size = 8, rounds = 65536) | |
| print("PBKDF2WithHmacSHA1 - 8 bytes salt") | |
| print(hash) | |
| print(binascii.b2a_hex(ab64_decode(hash.split('$')[-1].encode())).decode()) | |
| hash = pbkdf2_sha512.encrypt(key0, | |
| salt = "1234567812345678".encode(), salt_size = 16, rounds = 65536) | |
| print("PBKDF2WithHmacSHA512:") | |
| print(hash) | |
| print(binascii.b2a_hex(ab64_decode(hash.split('$')[-1].encode())).decode()) | |
| hash = pbkdf2_sha512.encrypt(key0, | |
| salt = "12345678".encode(), salt_size = 8, rounds = 65536) | |
| print("PBKDF2WithHmacSHA512 - 8 bytes salt") | |
| print(hash) | |
| print(binascii.b2a_hex(ab64_decode(hash.split('$')[-1].encode())).decode()) |
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
| Java: | |
| run: | |
| PBKDF2WithHmacSHA1:5B60E5A59D8C3AA55217578B108A28DC | |
| PBKDF2WithHmacSHA1 - 8 bytes salt:5AC8CBB67CACE91657C2FFA0AF384C20 | |
| PBKDF2WithHmacSHA512:77C8919FDEBE9BFE37F8F3FD6ADD6DF3 | |
| PBKDF2WithHmacSHA512 - 8 bytes salt:B0670D0BC077300749A74715F247BEC2 | |
| BUILD SUCCESSFUL (total time: 1 second) | |
| Python: | |
| 16 | |
| PBKDF2WithHmacSHA1: | |
| $pbkdf2$65536$MTIzNDU2NzgxMjM0NTY3OA$W2DlpZ2MOqVSF1eLEIoo3HyAvr0 | |
| 5b60e5a59d8c3aa55217578b108a28dc7c80bebd | |
| PBKDF2WithHmacSHA1 - 8 bytes salt | |
| $pbkdf2$65536$MTIzNDU2Nzg$WsjLtnys6RZXwv.grzhMIPGCD7s | |
| 5ac8cbb67cace91657c2ffa0af384c20f1820fbb | |
| PBKDF2WithHmacSHA512: | |
| $pbkdf2-sha512$65536$MTIzNDU2NzgxMjM0NTY3OA$d8iRn96.m/43.PP9at1t84vNUYjKAJPn6263ugpOSAtMf2xE2Um5BpcVju5XYDKQuo90WuS/b4KCF1jotC9Iwg | |
| 77c8919fdebe9bfe37f8f3fd6add6df38bcd5188ca0093e7eb6eb7ba0a4e480b4c7f6c44d949b90697158eee57603290ba8f745ae4bf6f82821758e8b42f48c2 | |
| PBKDF2WithHmacSHA512 - 8 bytes salt | |
| $pbkdf2-sha512$65536$MTIzNDU2Nzg$sGcNC8B3MAdJp0cV8ke.wgZXFyblkPkqamfptCGpZe7irorqnFkgwKGMtafLjTrdFmfbjLSvAafH5Go4Nq7oug | |
| b0670d0bc077300749a74715f247bec206571726e590f92a6a67e9b421a965eee2ae8aea9c5920c0a18cb5a7cb8d3add1667db8cb4af01a7c7e46a3836aee8ba |
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
| public class Utils { | |
| final protected static char[] hexArray = "0123456789ABCDEF".toCharArray(); | |
| public static String bytesToHex(byte[] bytes) { | |
| char[] hexChars = new char[bytes.length * 2]; | |
| for (int j = 0; j < bytes.length; j++) { | |
| int v = bytes[j] & 0xFF; | |
| hexChars[j * 2] = hexArray[v >>> 4]; | |
| hexChars[j * 2 + 1] = hexArray[v & 0x0F]; | |
| } | |
| return new String(hexChars); | |
| } | |
| public static byte[] hexToBytes(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; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment