forked from M66B/FairEmail
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
9,782 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package eu.faircode.email; | ||
|
||
/* | ||
This file is part of FairEmail. | ||
FairEmail 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. | ||
FairEmail 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 FairEmail. If not, see <http://www.gnu.org/licenses/>. | ||
Copyright 2018-2022 by Marcel Bokhorst (M66B) | ||
*/ | ||
|
||
import java.security.SecureRandom; | ||
|
||
import io.github.novacrypto.bip39.MnemonicGenerator; | ||
import io.github.novacrypto.bip39.Words; | ||
import io.github.novacrypto.bip39.wordlists.English; | ||
|
||
public class MnemonicHelper { | ||
// https://github.com/NovaCrypto/BIP39 | ||
|
||
static String get(byte[] entropy) { | ||
StringBuilder sb = new StringBuilder(); | ||
new SecureRandom().nextBytes(entropy); | ||
new MnemonicGenerator(English.INSTANCE).createMnemonic(entropy, sb::append); | ||
return sb.toString(); | ||
} | ||
|
||
static String get(String hex) { | ||
return get(fromHex(hex)); | ||
} | ||
|
||
static byte[] generate() { | ||
byte[] entropy = new byte[Words.TWELVE.byteLength()]; | ||
new SecureRandom().nextBytes(entropy); | ||
return entropy; | ||
} | ||
|
||
private static byte[] fromHex(String hex) { | ||
int len = hex.length(); | ||
byte[] data = new byte[len / 2]; | ||
for (int i = 0; i < len; i += 2) { | ||
data[i / 2] = | ||
(byte) ((Character.digit(hex.charAt(i), 16) << 4) + | ||
Character.digit(hex.charAt(i + 1), 16)); | ||
} | ||
return data; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
app/src/main/java/io/github/novacrypto/bip39/AlphabeticalCharSequenceComparator.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* BIP39 library, a Java implementation of BIP39 | ||
* Copyright (C) 2017-2019 Alan Evans, NovaCrypto | ||
* | ||
* 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 <https://www.gnu.org/licenses/>. | ||
* | ||
* Original source: https://github.com/NovaCrypto/BIP39 | ||
* You can contact the authors via github issues. | ||
*/ | ||
|
||
package io.github.novacrypto.bip39; | ||
|
||
import java.util.Comparator; | ||
|
||
enum CharSequenceComparators implements Comparator<CharSequence> { | ||
|
||
ALPHABETICAL { | ||
@Override | ||
public int compare(final CharSequence o1, final CharSequence o2) { | ||
final int length1 = o1.length(); | ||
final int length2 = o2.length(); | ||
final int length = Math.min(length1, length2); | ||
for (int i = 0; i < length; i++) { | ||
final int compare = Character.compare(o1.charAt(i), o2.charAt(i)); | ||
if (compare != 0) return compare; | ||
} | ||
return Integer.compare(length1, length2); | ||
} | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
app/src/main/java/io/github/novacrypto/bip39/ByteUtils.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* BIP39 library, a Java implementation of BIP39 | ||
* Copyright (C) 2017-2019 Alan Evans, NovaCrypto | ||
* | ||
* 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 <https://www.gnu.org/licenses/>. | ||
* | ||
* Original source: https://github.com/NovaCrypto/BIP39 | ||
* You can contact the authors via github issues. | ||
*/ | ||
|
||
package io.github.novacrypto.bip39; | ||
|
||
final class ByteUtils { | ||
|
||
static int next11Bits(byte[] bytes, int offset) { | ||
final int skip = offset / 8; | ||
final int lowerBitsToRemove = (3 * 8 - 11) - (offset % 8); | ||
return (((int) bytes[skip] & 0xff) << 16 | | ||
((int) bytes[skip + 1] & 0xff) << 8 | | ||
(lowerBitsToRemove < 8 | ||
? ((int) bytes[skip + 2] & 0xff) | ||
: 0)) >> lowerBitsToRemove & (1 << 11) - 1; | ||
} | ||
|
||
static void writeNext11(byte[] bytes, int value, int offset) { | ||
int skip = offset / 8; | ||
int bitSkip = offset % 8; | ||
{//byte 0 | ||
byte firstValue = bytes[skip]; | ||
byte toWrite = (byte) (value >> (3 + bitSkip)); | ||
bytes[skip] = (byte) (firstValue | toWrite); | ||
} | ||
|
||
{//byte 1 | ||
byte valueInByte = bytes[skip + 1]; | ||
final int i = 5 - bitSkip; | ||
byte toWrite = (byte) (i > 0 ? (value << i) : (value >> -i)); | ||
bytes[skip + 1] = (byte) (valueInByte | toWrite); | ||
} | ||
|
||
if (bitSkip >= 6) {//byte 2 | ||
byte valueInByte = bytes[skip + 2]; | ||
byte toWrite = (byte) (value << 13 - bitSkip); | ||
bytes[skip + 2] = (byte) (valueInByte | toWrite); | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
app/src/main/java/io/github/novacrypto/bip39/CharSequenceSplitter.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* BIP39 library, a Java implementation of BIP39 | ||
* Copyright (C) 2017-2019 Alan Evans, NovaCrypto | ||
* | ||
* 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 <https://www.gnu.org/licenses/>. | ||
* | ||
* Original source: https://github.com/NovaCrypto/BIP39 | ||
* You can contact the authors via github issues. | ||
*/ | ||
|
||
package io.github.novacrypto.bip39; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
final class CharSequenceSplitter { | ||
|
||
private final char separator1; | ||
private final char separator2; | ||
|
||
CharSequenceSplitter(final char separator1, final char separator2) { | ||
this.separator1 = separator1; | ||
this.separator2 = separator2; | ||
} | ||
|
||
List<CharSequence> split(final CharSequence charSequence) { | ||
final LinkedList<CharSequence> list = new LinkedList<>(); | ||
int start = 0; | ||
final int length = charSequence.length(); | ||
for (int i = 0; i < length; i++) { | ||
final char c = charSequence.charAt(i); | ||
if (c == separator1 || c == separator2) { | ||
list.add(charSequence.subSequence(start, i)); | ||
start = i + 1; | ||
} | ||
} | ||
list.add(charSequence.subSequence(start, length)); | ||
return list; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
app/src/main/java/io/github/novacrypto/bip39/JavaxPBKDF2WithHmacSHA512.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* BIP39 library, a Java implementation of BIP39 | ||
* Copyright (C) 2017-2019 Alan Evans, NovaCrypto | ||
* | ||
* 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 <https://www.gnu.org/licenses/>. | ||
* | ||
* Original source: https://github.com/NovaCrypto/BIP39 | ||
* You can contact the authors via github issues. | ||
*/ | ||
|
||
package io.github.novacrypto.bip39; | ||
|
||
import io.github.novacrypto.toruntime.CheckedExceptionToRuntime; | ||
|
||
import javax.crypto.SecretKey; | ||
import javax.crypto.SecretKeyFactory; | ||
import javax.crypto.spec.PBEKeySpec; | ||
|
||
import static io.github.novacrypto.toruntime.CheckedExceptionToRuntime.toRuntime; | ||
|
||
/** | ||
* Not available in all Java implementations, for example will not find the implementation before Android API 26+. | ||
* See https://developer.android.com/reference/javax/crypto/SecretKeyFactory.html for more details. | ||
*/ | ||
public enum JavaxPBKDF2WithHmacSHA512 implements PBKDF2WithHmacSHA512 { | ||
INSTANCE; | ||
|
||
private SecretKeyFactory skf = getPbkdf2WithHmacSHA512(); | ||
|
||
@Override | ||
public byte[] hash(char[] chars, byte[] salt) { | ||
final PBEKeySpec spec = new PBEKeySpec(chars, salt, 2048, 512); | ||
final byte[] encoded = generateSecretKey(spec).getEncoded(); | ||
spec.clearPassword(); | ||
return encoded; | ||
} | ||
|
||
private SecretKey generateSecretKey(final PBEKeySpec spec) { | ||
return toRuntime(new CheckedExceptionToRuntime.Func<SecretKey>() { | ||
@Override | ||
public SecretKey run() throws Exception { | ||
return skf.generateSecret(spec); | ||
} | ||
}); | ||
} | ||
|
||
private static SecretKeyFactory getPbkdf2WithHmacSHA512() { | ||
return toRuntime(new CheckedExceptionToRuntime.Func<SecretKeyFactory>() { | ||
@Override | ||
public SecretKeyFactory run() throws Exception { | ||
return SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512"); | ||
} | ||
}); | ||
} | ||
} |
Oops, something went wrong.