2016-11-28 15:57:57 +00:00
|
|
|
package com.luminiasoft.bitshares;
|
|
|
|
|
2016-11-29 03:46:59 +00:00
|
|
|
import com.google.gson.JsonArray;
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import com.google.gson.JsonParser;
|
2016-12-01 12:19:16 +00:00
|
|
|
import com.luminiasoft.bitshares.crypto.AndroidRandomSource;
|
2016-11-29 03:46:59 +00:00
|
|
|
import com.luminiasoft.bitshares.crypto.SecureRandomStrengthener;
|
|
|
|
import java.io.UnsupportedEncodingException;
|
2016-12-02 03:19:52 +00:00
|
|
|
import java.math.BigInteger;
|
2016-11-29 03:46:59 +00:00
|
|
|
import java.security.MessageDigest;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.security.SecureRandom;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import org.bitcoinj.core.ECKey;
|
|
|
|
import org.spongycastle.crypto.DataLengthException;
|
|
|
|
import org.spongycastle.crypto.InvalidCipherTextException;
|
|
|
|
import org.spongycastle.crypto.engines.AESFastEngine;
|
|
|
|
import org.spongycastle.crypto.modes.CBCBlockCipher;
|
|
|
|
import org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher;
|
|
|
|
import org.spongycastle.crypto.params.KeyParameter;
|
|
|
|
import org.spongycastle.crypto.params.ParametersWithIV;
|
2016-11-28 15:57:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class to manage the Bin Files
|
|
|
|
*
|
|
|
|
* @author Henry Varona
|
|
|
|
*/
|
|
|
|
public abstract class FileBin {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to get the brainkey fron an input of bytes
|
|
|
|
*
|
|
|
|
* @param input Array of bytes of the file to be processed
|
|
|
|
* @param password the pin code
|
|
|
|
* @return the brainkey file, or null if the file or the password are
|
|
|
|
* incorrect
|
|
|
|
*/
|
|
|
|
public static String getBrainkeyFromByte(byte[] input, String password) {
|
2016-12-01 20:53:03 +00:00
|
|
|
try {
|
2016-12-02 02:39:04 +00:00
|
|
|
byte[] publicKey = new byte[33];
|
2016-12-02 04:09:06 +00:00
|
|
|
byte[] rawDataEncripted = new byte[input.length-33];
|
2016-12-01 20:53:03 +00:00
|
|
|
|
|
|
|
System.arraycopy(input, 0, publicKey, 0, publicKey.length);
|
2016-12-02 04:09:06 +00:00
|
|
|
System.arraycopy(input, 33, rawDataEncripted, 0, rawDataEncripted.length);
|
2016-12-02 03:19:52 +00:00
|
|
|
|
2016-12-01 20:53:03 +00:00
|
|
|
MessageDigest md = MessageDigest.getInstance("SHA-256");
|
2016-12-02 02:39:04 +00:00
|
|
|
|
2016-12-01 20:53:03 +00:00
|
|
|
ECKey randomECKey = ECKey.fromPublicOnly(publicKey);
|
|
|
|
byte[] finalKey = randomECKey.getPubKeyPoint().multiply(ECKey.fromPrivate(md.digest(password.getBytes("UTF-8"))).getPrivKey()).normalize().getXCoord().getEncoded();
|
2016-12-01 21:33:23 +00:00
|
|
|
MessageDigest md1 = MessageDigest.getInstance("SHA-512");
|
|
|
|
finalKey = md1.digest(finalKey);
|
2016-12-05 01:23:52 +00:00
|
|
|
byte[] rawData = Util.decryptAES(rawDataEncripted, Util.byteToString(finalKey).getBytes());
|
2016-12-01 21:33:23 +00:00
|
|
|
|
2016-12-01 20:53:03 +00:00
|
|
|
byte[] checksum = new byte[4];
|
|
|
|
System.arraycopy(rawData, 0, checksum, 0, 4);
|
2016-12-02 03:19:52 +00:00
|
|
|
byte[] compressedData = new byte[rawData.length - 4];
|
2016-12-01 20:53:03 +00:00
|
|
|
System.arraycopy(rawData, 4, compressedData, 0, compressedData.length);
|
2016-12-02 04:09:06 +00:00
|
|
|
|
2016-12-02 19:17:26 +00:00
|
|
|
byte[] wallet_object_bytes = Util.decompress(compressedData, Util.XZ);
|
2016-12-02 03:19:52 +00:00
|
|
|
String wallet_string = new String(wallet_object_bytes, "UTF-8");
|
2016-12-02 02:39:04 +00:00
|
|
|
JsonObject wallet = new JsonParser().parse(wallet_string).getAsJsonObject();
|
2016-12-02 03:19:52 +00:00
|
|
|
if (wallet.get("wallet").isJsonArray()) {
|
|
|
|
wallet = wallet.get("wallet").getAsJsonArray().get(0).getAsJsonObject();
|
|
|
|
} else {
|
|
|
|
wallet = wallet.get("wallet").getAsJsonObject();
|
|
|
|
}
|
2016-12-04 16:29:43 +00:00
|
|
|
|
2016-12-02 03:19:52 +00:00
|
|
|
byte[] encKey_enc = new BigInteger(wallet.get("encryption_key").getAsString(), 16).toByteArray();
|
|
|
|
byte[] temp = new byte[encKey_enc.length - (encKey_enc[0] == 0 ? 1 : 0)];
|
|
|
|
System.arraycopy(encKey_enc, (encKey_enc[0] == 0 ? 1 : 0), temp, 0, temp.length);
|
2016-12-05 01:23:52 +00:00
|
|
|
byte[] encKey = Util.decryptAES(temp, password.getBytes("UTF-8"));
|
2016-12-04 16:29:43 +00:00
|
|
|
temp = new byte[encKey.length];
|
2016-12-02 03:19:52 +00:00
|
|
|
System.arraycopy(encKey, 0, temp, 0, temp.length);
|
|
|
|
|
|
|
|
byte[] encBrain = new BigInteger(wallet.get("encrypted_brainkey").getAsString(), 16).toByteArray();
|
|
|
|
while(encBrain[0] == 0){
|
|
|
|
byte[]temp2 = new byte[encBrain.length-1];
|
|
|
|
System.arraycopy(encBrain, 1, temp2, 0, temp2.length);
|
|
|
|
encBrain = temp2;
|
|
|
|
}
|
2016-12-05 01:23:52 +00:00
|
|
|
String BrainKey = new String((Util.decryptAES(encBrain, temp)), "UTF-8");
|
2016-12-02 03:19:52 +00:00
|
|
|
|
2016-12-02 02:39:04 +00:00
|
|
|
return BrainKey;
|
2016-12-02 03:19:52 +00:00
|
|
|
|
|
|
|
} catch (UnsupportedEncodingException | NoSuchAlgorithmException ex) {
|
|
|
|
|
2016-12-01 20:53:03 +00:00
|
|
|
}
|
2016-11-28 15:57:57 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to generate the file form a brainkey
|
|
|
|
*
|
|
|
|
* @param BrainKey The input brainkey
|
|
|
|
* @param password The pin code
|
2016-11-30 15:39:01 +00:00
|
|
|
* @param accountName The Account Name
|
|
|
|
* @return The array byte of the file, or null if an error happens
|
2016-11-28 15:57:57 +00:00
|
|
|
*/
|
2016-11-29 03:46:59 +00:00
|
|
|
public static byte[] getBytesFromBrainKey(String BrainKey, String password, String accountName) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
byte[] encKey = new byte[32];
|
|
|
|
SecureRandomStrengthener randomStrengthener = SecureRandomStrengthener.getInstance();
|
2016-12-02 04:09:06 +00:00
|
|
|
//randomStrengthener.addEntropySource(new AndroidRandomSource());
|
2016-11-29 03:46:59 +00:00
|
|
|
SecureRandom secureRandom = randomStrengthener.generateAndSeedRandomNumberGenerator();
|
|
|
|
secureRandom.nextBytes(encKey);
|
2016-12-05 01:23:52 +00:00
|
|
|
byte[] encKey_enc = Util.encryptAES(encKey, password.getBytes("UTF-8"));
|
|
|
|
byte[] encBrain = Util.encryptAES(BrainKey.getBytes("ASCII"), encKey);
|
2016-11-29 03:46:59 +00:00
|
|
|
|
2016-11-30 15:39:01 +00:00
|
|
|
/**
|
|
|
|
* Data to Store
|
|
|
|
*/
|
2016-11-29 03:46:59 +00:00
|
|
|
JsonObject wallet = new JsonObject();
|
2016-12-05 01:23:52 +00:00
|
|
|
wallet.add("encryption_key", new JsonParser().parse(Util.byteToString(encKey_enc)));
|
|
|
|
wallet.add("encrypted_brainkey", new JsonParser().parse(Util.byteToString(encBrain)));
|
2016-11-29 03:46:59 +00:00
|
|
|
JsonObject wallet_object = new JsonObject();
|
|
|
|
wallet_object.add("wallet", wallet);
|
|
|
|
JsonArray accountNames = new JsonArray();
|
|
|
|
JsonObject jsonAccountName = new JsonObject();
|
|
|
|
jsonAccountName.add("name", new JsonParser().parse(accountName));
|
|
|
|
accountNames.add(jsonAccountName);
|
|
|
|
wallet_object.add("linked_accounts", accountNames);
|
2016-12-02 19:17:26 +00:00
|
|
|
byte[] compressedData = Util.compress(wallet_object.toString().getBytes("UTF-8"), Util.XZ);
|
2016-11-29 03:46:59 +00:00
|
|
|
MessageDigest md = MessageDigest.getInstance("SHA-256");
|
|
|
|
byte[] checksum = md.digest(compressedData);
|
|
|
|
byte[] rawData = new byte[compressedData.length + 4];
|
|
|
|
System.arraycopy(checksum, 0, rawData, 0, 4);
|
|
|
|
System.arraycopy(compressedData, 0, rawData, 4, compressedData.length);
|
|
|
|
byte[] randomKey = new byte[32];
|
|
|
|
secureRandom.nextBytes(randomKey);
|
2016-11-29 19:04:52 +00:00
|
|
|
ECKey randomECKey = ECKey.fromPrivate(md.digest(randomKey));
|
|
|
|
byte[] randPubKey = randomECKey.getPubKey();
|
|
|
|
byte[] finalKey = randomECKey.getPubKeyPoint().multiply(ECKey.fromPrivate(md.digest(password.getBytes("UTF-8"))).getPrivKey()).normalize().getXCoord().getEncoded();
|
|
|
|
MessageDigest md1 = MessageDigest.getInstance("SHA-512");
|
|
|
|
finalKey = md1.digest(finalKey);
|
2016-12-05 01:23:52 +00:00
|
|
|
rawData = Util.encryptAES(rawData, Util.byteToString(finalKey).getBytes());
|
2016-12-02 04:09:06 +00:00
|
|
|
|
2016-11-29 19:04:52 +00:00
|
|
|
byte[] result = new byte[rawData.length + randPubKey.length];
|
|
|
|
System.arraycopy(randPubKey, 0, result, 0, randPubKey.length);
|
|
|
|
System.arraycopy(rawData, 0, result, randPubKey.length, rawData.length);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
} catch (UnsupportedEncodingException | NoSuchAlgorithmException ex) {
|
2016-11-30 15:39:01 +00:00
|
|
|
|
2016-11-29 03:46:59 +00:00
|
|
|
}
|
2016-11-28 15:57:57 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-12-05 01:23:52 +00:00
|
|
|
|
2016-11-28 15:57:57 +00:00
|
|
|
}
|