Skip to content

Commit

Permalink
Added constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
spoto committed Sep 13, 2023
1 parent f7670b4 commit 7a3b5e1
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public interface SignatureAlgorithm<T> {
*
* @param encoding the encoded version of the public key
* @return the public key
* @throws InvalidKeySpecException if the {@code encoded} key does not match the expected specification
* @throws InvalidKeySpecException if {@code encoding} does not match the expected specification
*/
PublicKey publicKeyFromEncoding(byte[] encoding) throws InvalidKeySpecException;

Expand All @@ -87,7 +87,7 @@ public interface SignatureAlgorithm<T> {
*
* @param encoding the encoded version of the private key
* @return the private key
* @throws InvalidKeySpecException if the {@code encoded} key does not match the expected specification
* @throws InvalidKeySpecException if {@code encoding} does not match the expected specification
*/
PrivateKey privateKeyFromEncoding(byte[] encoding) throws InvalidKeySpecException;

Expand Down
18 changes: 18 additions & 0 deletions io-hotmoka-crypto/src/main/java/io/hotmoka/crypto/Entropies.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.hotmoka.crypto;

import java.io.IOException;
import java.nio.file.Path;
import java.security.SecureRandom;

import io.hotmoka.crypto.api.Entropy;
Expand Down Expand Up @@ -55,10 +56,27 @@ public static Entropy random() {
* @return the entropy
* @throws IOException if the PEM file cannot be read
*/
@Deprecated
public static Entropy load(String filePrefix) throws IOException {
return new EntropyImpl(filePrefix);
}

/**
* Yields new entropy information read from a PEM file.
*
* @param path the file
* @return the entropy
* @throws IOException if the PEM file cannot be read
*/
public static Entropy load(Path path) throws IOException {
long length = path.toFile().length();
// without this check, the access to the file would take very long and terminate with an error anyway
if (length > 1000L)
throw new IOException("The pem file " + path + " is too long for being a PEM file!");

return new EntropyImpl(path);
}

/**
* Yields a copy of the given entropy.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,16 @@ public Stream<String> stream() {
@Override
public <R extends Comparable<? super R>> Account<R> toAccount(BiFunction<Entropy, byte[], Account<R>> accountCreator) {
// each mnemonic word represents 11 bits
boolean[] bits = new boolean[words.length * 11];
var bits = new boolean[words.length * 11];

// the transaction is always 32 bytes long
byte[] transaction = new byte[32];
var transaction = new byte[32];

int bitsOfChecksum = words.length / 3;
boolean[] checksum = new boolean[bitsOfChecksum];
var checksum = new boolean[bitsOfChecksum];

// the entropy uses the remaining number of bytes
byte[] entropy = new byte[(bits.length - bitsOfChecksum) / 8 - transaction.length];
var entropy = new byte[(bits.length - bitsOfChecksum) / 8 - transaction.length];

int startOfTransaction = entropy.length * 8;
int startOfChecksum = startOfTransaction + transaction.length * 8;
Expand Down Expand Up @@ -138,11 +138,11 @@ public <R extends Comparable<? super R>> Account<R> toAccount(BiFunction<Entropy
throw new RuntimeException("unexpected exception", e);
}

byte[] merge = new byte[entropy.length + transaction.length];
var merge = new byte[entropy.length + transaction.length];
System.arraycopy(entropy, 0, merge, 0, entropy.length);
System.arraycopy(transaction, 0, merge, entropy.length, transaction.length);
byte[] sha256 = digest.digest(merge);
boolean[] checksumRecomputed = new boolean[bitsOfChecksum];
var checksumRecomputed = new boolean[bitsOfChecksum];
for (pos = 0; pos < bitsOfChecksum; pos++)
checksumRecomputed[pos] = (sha256[pos] & (0x80 >>> (pos % 8))) != 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ public EntropyImpl(String filePrefix) throws IOException {
throw new IllegalArgumentException("illegal entropy length: 16 bytes expected");
}

/**
* Reads the entropy information from a PEM file.
*
* @param path the file
* @throws IOException if the PEM file cannot be read
*/
public EntropyImpl(Path path) throws IOException {
try (var reader = new PemReader(new FileReader(path.toFile()))) {
entropy = reader.readPemObject().getContent();
}

if (entropy.length != 16)
throw new IllegalArgumentException("illegal entropy length: 16 bytes expected");
}

/**
* Copy constructor.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

import io.hotmoka.marshalling.api.Marshallable;
Expand Down Expand Up @@ -69,8 +70,7 @@ protected void registerObjectUnmarshaller(ObjectUnmarshaller<?> ou) {
public <C> C readObject(Class<C> clazz) throws IOException {
@SuppressWarnings("unchecked")
var ou = (ObjectUnmarshaller<C>) objectUnmarshallers.get(clazz);
if (ou == null)
throw new IllegalStateException("missing object unmarshaller for class " + clazz.getName());
Objects.requireNonNull(ou, "Missing object unmarshaller for class " + clazz.getName());

return ou.read(this);
}
Expand Down

0 comments on commit 7a3b5e1

Please sign in to comment.