Search in sources :

Example 11 with Bytes

use of net.sergeych.utils.Bytes in project universa by UniversaBlockchain.

the class KeyRecord method setupKey.

private void setupKey() {
    try {
        Object x = getOrThrow("key");
        remove("key");
        if (x instanceof PublicKey) {
            publicKey = (PublicKey) x;
        } else if (x instanceof PrivateKey) {
            publicKey = ((PrivateKey) x).getPublicKey();
        } else if (x instanceof String) {
            publicKey = new PublicKey(Base64u.decodeCompactString((String) x));
        } else {
            if (x instanceof Bytes)
                x = ((Bytes) x).toArray();
            if (x instanceof byte[]) {
                publicKey = new PublicKey((byte[]) x);
            } else {
                throw new IllegalArgumentException("unsupported key object: " + x.getClass().getName());
            }
        }
        put("key", publicKey);
    } catch (EncryptionError e) {
        throw new IllegalArgumentException("unsupported key, failed to construct", e);
    }
}
Also used : Bytes(net.sergeych.utils.Bytes) PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) EncryptionError(com.icodici.crypto.EncryptionError)

Example 12 with Bytes

use of net.sergeych.utils.Bytes in project universa by UniversaBlockchain.

the class BiMapperTest method processBytes.

@Test
public void processBytes() throws Exception {
    byte[] x = Do.randomBytes(10);
    Binder s = DefaultBiMapper.serialize(x);
    byte[] result = ((Bytes) DefaultBiMapper.deserialize(s)).getData();
    assertArrayEquals(x, result);
}
Also used : Binder(net.sergeych.tools.Binder) Bytes(net.sergeych.utils.Bytes) Test(org.junit.Test)

Example 13 with Bytes

use of net.sergeych.utils.Bytes in project universa by UniversaBlockchain.

the class UnikeyFactoryTest method testUnikeyStructure.

@Test
public void testUnikeyStructure() throws Exception {
    assertEquals(524, pk1Bytes.length);
    assertEquals(3, pk1E.length);
    assertEquals(256, pk1P.length);
    assertEquals(256, pk1Q.length);
    // Private key structure
    // It contains 4 components...
    final ArrayList unpackedFromBoss = Boss.load(pk1Bytes);
    assertEquals(4, unpackedFromBoss.size());
    // First one is Integer, other ones are Bytes.
    assertTrue(unpackedFromBoss.get(0) instanceof Integer);
    assertTrue(unpackedFromBoss.get(1) instanceof Bytes);
    assertTrue(unpackedFromBoss.get(2) instanceof Bytes);
    assertTrue(unpackedFromBoss.get(3) instanceof Bytes);
    // Now their contents...
    assertEquals(0, unpackedFromBoss.get(0));
    assertArrayEquals(pk1E, ((Bytes) unpackedFromBoss.get(1)).toArray());
    assertArrayEquals(pk1P, ((Bytes) unpackedFromBoss.get(2)).toArray());
    assertArrayEquals(pk1Q, ((Bytes) unpackedFromBoss.get(3)).toArray());
}
Also used : Bytes(net.sergeych.utils.Bytes) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 14 with Bytes

use of net.sergeych.utils.Bytes in project universa by UniversaBlockchain.

the class Boss method traceObject.

private static String traceObject(String prefix, Object obj) {
    if (obj instanceof Bytes || obj instanceof byte[]) {
        Bytes bb = obj instanceof Bytes ? (Bytes) obj : new Bytes((byte[]) obj);
        if (bb.size() > 30)
            return prefix + bb.part(0, 30).toHex() + String.format(" ...(%d bytes)\n", bb.size());
        return prefix + bb.toHex() + "\n";
    }
    if (obj instanceof Object[] || obj instanceof List<?>) {
        return traceArray(prefix, Do.collection(obj));
    }
    if (obj instanceof Binder) {
        Binder i = (Binder) obj;
        StringBuilder b = new StringBuilder();
        for (Map.Entry<String, Object> e : i.entrySet()) {
            b.append(prefix + e.getKey() + ":\n");
            b.append(traceObject(prefix + "  ", e.getValue()));
        }
        return b.toString();
    }
    return prefix + (obj == null ? "null" : "\"" + obj.toString() + "\"") + "\n";
}
Also used : Bytes(net.sergeych.utils.Bytes) Binder(net.sergeych.tools.Binder)

Aggregations

Bytes (net.sergeych.utils.Bytes)14 Binder (net.sergeych.tools.Binder)7 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)3 PrivateKey (com.icodici.crypto.PrivateKey)2 PublicKey (com.icodici.crypto.PublicKey)2 EncryptionError (com.icodici.crypto.EncryptionError)1 Sha3_384 (com.icodici.crypto.digest.Sha3_384)1 Sha512 (com.icodici.crypto.digest.Sha512)1 HashId (com.icodici.universa.HashId)1 Quantiser (com.icodici.universa.node2.Quantiser)1 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 SecureRandom (java.security.SecureRandom)1 Arrays.asList (java.util.Arrays.asList)1 List (java.util.List)1 Map (java.util.Map)1 Nullable (org.checkerframework.checker.nullness.qual.Nullable)1