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
open class SingletonHolder<out T, in A>(private val constructor: (A) -> T) { | |
@Volatile | |
private var instance: T? = null | |
fun getInstance(arg: A): T { | |
return when { | |
instance != null -> instance!! | |
else -> synchronized(this) { | |
if (instance == null) instance = constructor(arg) |
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 EncryptorAesGcmPasswordFile{ | |
public static byte[] encrypt(byte[] pText, String password) throws Exception { | |
//... | |
// prefix IV and Salt to cipher text | |
byte[] cipherTextWithIvSalt = ByteBuffer.allocate(iv.length + salt.length + cipherText.length) | |
.put(iv) | |
.put(salt) | |
.put(cipherText) |
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
package com.mkyong.crypto.utils; | |
import javax.crypto.KeyGenerator; | |
import javax.crypto.SecretKey; | |
import javax.crypto.SecretKeyFactory; | |
import javax.crypto.spec.PBEKeySpec; | |
import javax.crypto.spec.SecretKeySpec; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.SecureRandom; |
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
package com.mkyong.crypto.encryptor; | |
import com.mkyong.crypto.utils.CryptoUtils; | |
import javax.crypto.Cipher; | |
import javax.crypto.SecretKey; | |
import javax.crypto.spec.GCMParameterSpec; | |
import java.nio.ByteBuffer; | |
import java.nio.charset.Charset; |
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
package com.mkyong.crypto.encryptor; | |
import com.mkyong.crypto.utils.CryptoUtils; | |
import javax.crypto.Cipher; | |
import javax.crypto.SecretKey; | |
import javax.crypto.spec.GCMParameterSpec; | |
import java.nio.ByteBuffer; | |
import java.nio.charset.Charset; | |
import java.nio.charset.StandardCharsets; |
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 javax.crypto.Cipher; | |
import javax.crypto.KeyGenerator; | |
import javax.crypto.SecretKey; | |
import javax.crypto.spec.GCMParameterSpec; | |
import java.util.Base64; | |
/** | |
* Possible KEY_SIZE values are 128, 192 and 256 | |
* Possible T_LEN values are 128, 120, 112, 104 and 96 | |
*/ |
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 javax.crypto.Cipher; | |
import java.security.KeyPair; | |
import java.security.KeyPairGenerator; | |
import java.security.PrivateKey; | |
import java.security.PublicKey; | |
import java.util.Base64; | |
/** | |
* @author Anass AIT BEN EL ARBI | |
* <ul> |