Add Numeric9x4PassphraseUtils
This commit is contained in:
@@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2017 Schürmann & Breitmoser GbR
|
||||||
|
*
|
||||||
|
* This program 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.
|
||||||
|
*
|
||||||
|
* This program 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 should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.sufficientlysecure.keychain.util;
|
||||||
|
|
||||||
|
|
||||||
|
import java.nio.CharBuffer;
|
||||||
|
import java.security.SecureRandom;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import android.support.annotation.VisibleForTesting;
|
||||||
|
|
||||||
|
// see https://autocrypt.org/level1.html#setup-code
|
||||||
|
public class Numeric9x4PassphraseUtil {
|
||||||
|
public static Passphrase generateNumeric9x4Passphrase() {
|
||||||
|
return generateNumeric9x4Passphrase(new SecureRandom());
|
||||||
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
static Passphrase generateNumeric9x4Passphrase(Random r) {
|
||||||
|
StringBuilder code = new StringBuilder(36);
|
||||||
|
for (int i = 0; i < 36; i++) {
|
||||||
|
boolean isBeginningOfBlock = i > 0 && (i % 4) == 0;
|
||||||
|
if (isBeginningOfBlock) {
|
||||||
|
code.append('-');
|
||||||
|
}
|
||||||
|
|
||||||
|
String digit = Integer.toString(r.nextInt(10));
|
||||||
|
code.append(digit);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Passphrase(code.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final Pattern AUTOCRYPT_TRANSFER_CODE = Pattern.compile("(\\d{4}-){8}\\d{4}");
|
||||||
|
|
||||||
|
public static boolean isNumeric9x4Passphrase(Passphrase transferCodeChars) {
|
||||||
|
return isNumeric9x4Passphrase(CharBuffer.wrap(transferCodeChars.getCharArray()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isNumeric9x4Passphrase(CharSequence code) {
|
||||||
|
return AUTOCRYPT_TRANSFER_CODE.matcher(code).matches();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package org.sufficientlysecure.keychain.util;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
|
||||||
|
public class Numeric9x4PassphraseUtilTest {
|
||||||
|
private static final int RANDOM_SEED = 12345;
|
||||||
|
private static final String TRANSFER_CODE_3x3x4 = "1018-5452-1972-0624-7325-1126-8153-1997-1535";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void generateNumeric9x4Passphrase() {
|
||||||
|
Random r = new Random(RANDOM_SEED);
|
||||||
|
|
||||||
|
Passphrase passphrase = Numeric9x4PassphraseUtil.generateNumeric9x4Passphrase(r);
|
||||||
|
|
||||||
|
assertEquals(TRANSFER_CODE_3x3x4, passphrase.toStringUnsafe());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isNumeric9x4Passphrase() {
|
||||||
|
boolean isValidCodeLayout = Numeric9x4PassphraseUtil.isNumeric9x4Passphrase(TRANSFER_CODE_3x3x4);
|
||||||
|
|
||||||
|
assertTrue(isValidCodeLayout);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isNumeric9x4Passphrase_withBadSuffix() {
|
||||||
|
boolean isValidCodeLayout = Numeric9x4PassphraseUtil.isNumeric9x4Passphrase(TRANSFER_CODE_3x3x4 + "x");
|
||||||
|
|
||||||
|
assertFalse(isValidCodeLayout);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user