use of net.viperfish.journal.framework.errors.FailToSyncCipherDataException in project vsDiaryWriter by shilongdai.
the class StreamCipherTransformer method setPassword.
@Override
public void setPassword(String pass) throws FailToSyncCipherDataException {
if (salts == null) {
byte[] macSalt = new byte[8];
// left blank intentionally
byte[] encSalt = new byte[8];
rand.nextBytes(macSalt);
salts = new ByteParameterPair(encSalt, macSalt);
try {
Files.write(additSalt.toPath(), salts.toString().getBytes(StandardCharsets.US_ASCII), StandardOpenOption.WRITE, StandardOpenOption.CREATE);
} catch (IOException e) {
FailToSyncCipherDataException fc = new FailToSyncCipherDataException("Cannot write salt to " + additSalt.getAbsolutePath() + " message:" + e.getMessage());
fc.initCause(e);
throw fc;
}
}
masterKey = this.generateKey(pass);
hkdf.init(new HKDFParameters(masterKey, salts.getSecond(), "Mac Key".getBytes()));
byte[] macKey = new byte[this.getMacKeySize() / 8];
hkdf.generateBytes(macKey, 0, macKey.length);
this.initMac(macKey);
}
use of net.viperfish.journal.framework.errors.FailToSyncCipherDataException in project vsDiaryWriter by shilongdai.
the class StreamCipherTransformer method loadSalt.
void loadSalt() {
if (additSalt.exists()) {
try {
byte[] raw = Files.readAllBytes(additSalt.toPath());
String encoded = new String(raw, StandardCharsets.US_ASCII);
if (encoded.length() != 0)
this.salts = ByteParameterPair.valueOf(encoded);
} catch (IOException e) {
FailToSyncCipherDataException fc = new FailToSyncCipherDataException("Cannot load salt from:" + additSalt.getAbsolutePath() + " message:" + e.getMessage());
fc.initCause(e);
throw new RuntimeException(fc);
}
}
}
use of net.viperfish.journal.framework.errors.FailToSyncCipherDataException in project vsDiaryWriter by shilongdai.
the class BlockCipherMacTransformer method setPassword.
/**
* derive the key from the password
*
* @throws FailToSyncCipherDataException
*/
@Override
public void setPassword(String string) throws FailToSyncCipherDataException {
try {
String saltRaw = salts.read(StandardCharsets.US_ASCII);
if (saltRaw.length() > 0) {
saltPair = ByteParameterPair.valueOf(saltRaw);
} else {
byte[] encSalt = new byte[8];
byte[] macSalt = new byte[8];
rand.nextBytes(encSalt);
rand.nextBytes(macSalt);
saltPair = new ByteParameterPair(encSalt, macSalt);
salts.write(saltPair.toString(), StandardCharsets.US_ASCII);
}
} catch (IOException e) {
FailToSyncCipherDataException fc = new FailToSyncCipherDataException("Cannot load encryption and mac salt from file");
fc.initCause(e);
throw fc;
}
this.key = generateKey(string);
hkdf.init(new HKDFParameters(key, saltPair.getFirst(), "Encryption Key".getBytes()));
cryptKey = new byte[enc.getKeySize() / 8];
hkdf.generateBytes(cryptKey, 0, cryptKey.length);
byte[] macKey = new byte[getMacKeySize() / 8];
hkdf.init(new HKDFParameters(key, saltPair.getSecond(), "Mac Key".getBytes()));
hkdf.generateBytes(macKey, 0, macKey.length);
initMac(macKey);
}
use of net.viperfish.journal.framework.errors.FailToSyncCipherDataException in project vsDiaryWriter by shilongdai.
the class CompressMacTest method testWrapper.
protected void testWrapper() {
JournalTransformer wrapper;
try {
CommonFunctions.initDir(testDir);
wrapper = createTransformer(new File(testDir.getCanonicalPath() + "/salt"));
} catch (IOException e) {
System.err.println("cannot load salt");
throw new RuntimeException(e);
}
try {
wrapper.setPassword("password");
} catch (FailToSyncCipherDataException e1) {
throw new RuntimeException(e1);
}
Journal j = new Journal();
j.setSubject("test get");
j.setContent("test get content");
try {
Journal result = wrapper.encryptJournal(j);
result = wrapper.decryptJournal(result);
String plainContent = result.getContent();
Assert.assertEquals("test get content", plainContent);
Assert.assertEquals("test get", result.getSubject());
} catch (CipherException | CompromisedDataException e) {
throw new RuntimeException(e);
}
}
Aggregations