2
0

Initial Java JSON serialization/deserialization.

This commit is contained in:
Maarten Billemont
2018-05-08 22:40:48 -04:00
parent 1cb720da32
commit f0d523fb35
26 changed files with 330 additions and 287 deletions

View File

@@ -59,7 +59,12 @@ public abstract class MPAlgorithm {
/**
* mpw: defaults: password result type.
*/
public abstract MPResultType mpw_default_type();
public abstract MPResultType mpw_default_password_type();
/**
* mpw: defaults: login result type.
*/
public abstract MPResultType mpw_default_login_type();
/**
* mpw: defaults: initial counter value.

View File

@@ -83,8 +83,8 @@ public class MPAlgorithmV0 extends MPAlgorithm {
}
@Override
public byte[] siteKey(final byte[] masterKey, final String siteName, UnsignedInteger siteCounter, final MPKeyPurpose keyPurpose,
@Nullable final String keyContext) {
public byte[] siteKey(final byte[] masterKey, final String siteName, UnsignedInteger siteCounter,
final MPKeyPurpose keyPurpose, @Nullable final String keyContext) {
String keyScope = keyPurpose.getScope();
logger.trc( "keyScope: %s", keyScope );
@@ -117,8 +117,8 @@ public class MPAlgorithmV0 extends MPAlgorithm {
@Override
public String siteResult(final byte[] masterKey, final byte[] siteKey, final String siteName, final UnsignedInteger siteCounter,
final MPKeyPurpose keyPurpose,
@Nullable final String keyContext, final MPResultType resultType, @Nullable final String resultParam) {
final MPKeyPurpose keyPurpose, @Nullable final String keyContext,
final MPResultType resultType, @Nullable final String resultParam) {
switch (resultType.getTypeClass()) {
case Template:
@@ -133,8 +133,8 @@ public class MPAlgorithmV0 extends MPAlgorithm {
}
@Override
public String sitePasswordFromTemplate(final byte[] masterKey, final byte[] siteKey, final MPResultType resultType,
@Nullable final String resultParam) {
public String sitePasswordFromTemplate(final byte[] masterKey, final byte[] siteKey,
final MPResultType resultType, @Nullable final String resultParam) {
int[] _siteKey = new int[siteKey.length];
for (int i = 0; i < siteKey.length; ++i) {
@@ -168,8 +168,8 @@ public class MPAlgorithmV0 extends MPAlgorithm {
}
@Override
public String sitePasswordFromCrypt(final byte[] masterKey, final byte[] siteKey, final MPResultType resultType,
@Nullable final String resultParam) {
public String sitePasswordFromCrypt(final byte[] masterKey, final byte[] siteKey,
final MPResultType resultType, @Nullable final String resultParam) {
Preconditions.checkNotNull( resultParam );
Preconditions.checkArgument( !resultParam.isEmpty() );
@@ -192,8 +192,8 @@ public class MPAlgorithmV0 extends MPAlgorithm {
}
@Override
public String sitePasswordFromDerive(final byte[] masterKey, final byte[] siteKey, final MPResultType resultType,
@Nullable final String resultParam) {
public String sitePasswordFromDerive(final byte[] masterKey, final byte[] siteKey,
final MPResultType resultType, @Nullable final String resultParam) {
if (resultType == MPResultType.DeriveKey) {
int resultParamInt = ConversionUtils.toIntegerNN( resultParam );
@@ -220,8 +220,8 @@ public class MPAlgorithmV0 extends MPAlgorithm {
@Override
public String siteState(final byte[] masterKey, final byte[] siteKey, final String siteName, final UnsignedInteger siteCounter,
final MPKeyPurpose keyPurpose,
@Nullable final String keyContext, final MPResultType resultType, final String resultParam) {
final MPKeyPurpose keyPurpose, @Nullable final String keyContext,
final MPResultType resultType, final String resultParam) {
try {
// Encrypt
@@ -246,111 +246,77 @@ public class MPAlgorithmV0 extends MPAlgorithm {
return MPMasterKey.Version.V0;
}
/**
* mpw: defaults: password result type.
*/
@Override
public MPResultType mpw_default_type() {
public MPResultType mpw_default_password_type() {
return MPResultType.GeneratedLong;
}
/**
* mpw: defaults: initial counter value.
*/
@Override
public MPResultType mpw_default_login_type() {
return MPResultType.GeneratedName;
}
@Override
public UnsignedInteger mpw_default_counter() {
return UnsignedInteger.ONE;
}
/**
* mpw: validity for the time-based rolling counter.
*/
@Override
@SuppressWarnings("MagicNumber")
public long mpw_otp_window() {
return 5 * 60 /* s */;
}
/**
* mpw: Key ID hash.
*/
@Override
public MessageDigests mpw_hash() {
return MessageDigests.SHA256;
}
/**
* mpw: Site digest.
*/
@Override
public MessageAuthenticationDigests mpw_digest() {
return MessageAuthenticationDigests.HmacSHA256;
}
/**
* mpw: Platform-agnostic byte order.
*/
@Override
public ByteOrder mpw_byteOrder() {
return ByteOrder.BIG_ENDIAN;
}
/**
* mpw: Input character encoding.
*/
@Override
public Charset mpw_charset() {
return Charsets.UTF_8;
}
/**
* mpw: Master key size (byte).
*/
@Override
@SuppressWarnings("MagicNumber")
public int mpw_dkLen() {
return 64;
}
/**
* mpw: Minimum size for derived keys (bit).
*/
@Override
@SuppressWarnings("MagicNumber")
public int mpw_keySize_min() {
return 128;
}
/**
* mpw: Maximum size for derived keys (bit).
*/
@Override
@SuppressWarnings("MagicNumber")
public int mpw_keySize_max() {
return 512;
}
/**
* scrypt: Parallelization parameter.
*/
@Override
@SuppressWarnings("MagicNumber")
public int scrypt_p() {
return 2;
}
/**
* scrypt: Memory cost parameter.
*/
@Override
@SuppressWarnings("MagicNumber")
public int scrypt_r() {
return 8;
}
/**
* scrypt: CPU cost parameter.
*/
@Override
@SuppressWarnings("MagicNumber")
public int scrypt_N() {

View File

@@ -42,5 +42,5 @@ public final class MPConstant {
public static final int MS_PER_S = 1000;
public static final DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTimeNoMillis();
public static final DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTimeNoMillis().withZoneUTC();
}

View File

@@ -1,25 +0,0 @@
//==============================================================================
// This file is part of Master Password.
// Copyright (c) 2011-2017, Maarten Billemont.
//
// Master Password is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Master Password is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You can find a copy of the GNU General Public License in the
// LICENSE file. Alternatively, see <http://www.gnu.org/licenses/>.
//==============================================================================
package com.lyndir.masterpassword;
/**
* @author lhunath, 2017-09-21
*/
public class MPInvalidatedException extends Exception {
}

View File

@@ -56,14 +56,14 @@ public class MPMasterKey {
/**
* Derive the master key for a user based on their name and master password.
*
* @throws MPInvalidatedException {@link #invalidate()} has been called on this object.
* @throws MPKeyUnavailableException {@link #invalidate()} has been called on this object.
*/
private byte[] masterKey(final MPAlgorithm algorithm)
throws MPInvalidatedException {
throws MPKeyUnavailableException {
Preconditions.checkArgument( masterPassword.length > 0 );
if (invalidated)
throw new MPInvalidatedException();
throw new MPKeyUnavailableException();
byte[] key = keyByVersion.get( algorithm.version() );
if (key == null) {
@@ -81,11 +81,11 @@ public class MPMasterKey {
/**
* Derive the master key for a user based on their name and master password.
*
* @throws MPInvalidatedException {@link #invalidate()} has been called on this object.
* @throws MPKeyUnavailableException {@link #invalidate()} has been called on this object.
*/
private byte[] siteKey(final String siteName, final UnsignedInteger siteCounter, final MPKeyPurpose keyPurpose,
@Nullable final String keyContext, final MPAlgorithm algorithm)
throws MPInvalidatedException {
throws MPKeyUnavailableException {
Preconditions.checkArgument( !siteName.isEmpty() );
byte[] masterKey = masterKey( algorithm );
@@ -110,12 +110,12 @@ public class MPMasterKey {
* @param resultParam A parameter for the resultType. For stateful result types, the output of
* {@link #siteState(String, UnsignedInteger, MPKeyPurpose, String, MPResultType, String, MPAlgorithm)}.
*
* @throws MPInvalidatedException {@link #invalidate()} has been called on this object.
* @throws MPKeyUnavailableException {@link #invalidate()} has been called on this object.
*/
public String siteResult(final String siteName, final UnsignedInteger siteCounter, final MPKeyPurpose keyPurpose,
@Nullable final String keyContext, final MPResultType resultType, @Nullable final String resultParam,
final MPAlgorithm algorithm)
throws MPInvalidatedException {
throws MPKeyUnavailableException {
byte[] masterKey = masterKey( algorithm );
byte[] siteKey = siteKey( siteName, siteCounter, keyPurpose, keyContext, algorithm );
@@ -139,12 +139,12 @@ public class MPMasterKey {
* @param resultParam The result token desired from
* {@link #siteResult(String, UnsignedInteger, MPKeyPurpose, String, MPResultType, String, MPAlgorithm)}.
*
* @throws MPInvalidatedException {@link #invalidate()} has been called on this object.
* @throws MPKeyUnavailableException {@link #invalidate()} has been called on this object.
*/
public String siteState(final String siteName, final UnsignedInteger siteCounter, final MPKeyPurpose keyPurpose,
@Nullable final String keyContext, final MPResultType resultType, @Nullable final String resultParam,
final MPAlgorithm algorithm)
throws MPInvalidatedException {
throws MPKeyUnavailableException {
Preconditions.checkNotNull( resultParam );
Preconditions.checkArgument( !resultParam.isEmpty() );
@@ -169,10 +169,10 @@ public class MPMasterKey {
/**
* Calculate an identifier for the master key.
*
* @throws MPInvalidatedException {@link #invalidate()} has been called on this object.
* @throws MPKeyUnavailableException {@link #invalidate()} has been called on this object.
*/
public byte[] getKeyID(final MPAlgorithm algorithm)
throws MPInvalidatedException {
throws MPKeyUnavailableException {
return algorithm.toID( masterKey( algorithm ) );
}