Java Code Examples for co.nstant.in.cbor.CborBuilder

The following examples show how to use co.nstant.in.cbor.CborBuilder. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
/**
 * Encode this self-attestation attObj as the CBOR required by the WebAuthn spec
 * https://www.w3.org/TR/webauthn/#sctn-attestation
 * https://www.w3.org/TR/webauthn/#packed-attestation
 *
 * @return CBOR encoding of the attestation object as a byte array
 * @throws VirgilException
 */
@Override
public byte[] asCBOR() throws VirgilException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        new CborEncoder(baos).encode(new CborBuilder()
                .addMap()
                .put("authData", this.authData)
                .put("fmt", "packed")
                .putMap("attStmt")
                .put("alg", (long) -7)
                .put("sig", this.signature)
                .end()
                .end()
                .build()
        );
    } catch (CborException e) {
        throw new VirgilException("couldn't serialize to cbor", e);
    }
    return baos.toByteArray();
}
 
Example #2
/**
 * Encode this self-attestation attObj as the CBOR required by the WebAuthn spec
 * https://www.w3.org/TR/webauthn/#sctn-attestation
 * https://www.w3.org/TR/webauthn/#none-attestation
 *
 * @return CBOR encoding of the attestation object as a byte array
 * @throws VirgilException
 */
@Override
public byte[] asCBOR() throws VirgilException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        new CborEncoder(baos).encode(new CborBuilder()
                .addMap()
                .put("authData", this.authData)
                .put("fmt", "none")
                .putMap("attStmt")
                .end()
                .end()
                .build()
        );
    } catch (CborException e) {
        throw new VirgilException("couldn't serialize to cbor", e);
    }
    return baos.toByteArray();
}
 
Example #3
Source Project: amazon-freertos-ble-android-sdk   Author: aws   File: DeleteNetworkReq.java    License: Apache License 2.0 6 votes vote down vote up
public byte[] encode() {
    byte[] DeleteNetworkRequestBytes = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new CborEncoder(baos).encode(new CborBuilder()
                .addMap()
                .put(TYPE_KEY, DELETE_NETWORK_REQ)
                .put(INDEX_KEY, index)
                .end()
                .build());
        DeleteNetworkRequestBytes = baos.toByteArray();
    } catch (CborException e) {
        Log.e(TAG, "Failed to encode.", e);
    }
    return DeleteNetworkRequestBytes;
}
 
Example #4
Source Project: amazon-freertos-ble-android-sdk   Author: aws   File: EditNetworkReq.java    License: Apache License 2.0 6 votes vote down vote up
public byte[] encode() {
    byte[] EditNetworkRequestBytes = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new CborEncoder(baos).encode(new CborBuilder()
                .addMap()
                .put(TYPE_KEY, EDIT_NETWORK_REQ)
                .put(INDEX_KEY, index)
                .put(NEWINDEX_KEY, newIndex)
                .end()
                .build());
        EditNetworkRequestBytes = baos.toByteArray();
    } catch (CborException e) {
        Log.e(TAG, "Failed to encode.", e);
    }
    return EditNetworkRequestBytes;
}
 
Example #5
Source Project: amazon-freertos-ble-android-sdk   Author: aws   File: ListNetworkReq.java    License: Apache License 2.0 6 votes vote down vote up
public byte[] encode() {
    byte[] ListNetworkRequestBytes = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new CborEncoder(baos).encode(new CborBuilder()
                .addMap()
                .put(TYPE_KEY, LIST_NETWORK_REQ)
                .put(MAXNETWORKS_KEY, maxNetworks)
                .put(TIMEOUT_KEY, timeout)
                .end()
                .build());
        ListNetworkRequestBytes = baos.toByteArray();
    } catch (CborException e) {
        Log.e(TAG, "Failed to encode.", e);
    }
    return ListNetworkRequestBytes;
}
 
Example #6
Source Project: amazon-freertos-ble-android-sdk   Author: aws   File: SaveNetworkReq.java    License: Apache License 2.0 6 votes vote down vote up
public byte[] encode() {
    byte[] SaveNetworkRequestBytes = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new CborEncoder(baos).encode(new CborBuilder()
                .addMap()
                .put(TYPE_KEY, SAVE_NETWORK_REQ)
                .put(INDEX_KEY, index)
                .put(SSID_KEY, ssid)
                .put(BSSID_KEY, bssid)
                .put(PSK_KEY, psk)
                .put(SECURITY_KEY, security)
                .end()
                .build());
        SaveNetworkRequestBytes = baos.toByteArray();
    } catch (CborException e) {
        Log.e(TAG, "Failed to encode.", e);
    }
    return SaveNetworkRequestBytes;
}
 
Example #7
Source Project: amazon-freertos-ble-android-sdk   Author: aws   File: Puback.java    License: Apache License 2.0 6 votes vote down vote up
public byte[] encode() {
    byte[] pubackBytes = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new CborEncoder(baos).encode(new CborBuilder()
            .addMap()
            .put(TYPE_KEY, type)
            .put(MSGID_KEY, msgID)
            .end()
            .build());
        pubackBytes = baos.toByteArray();
    } catch (CborException e) {
        Log.e(TAG, "Failed to encode.", e);
    }
    return pubackBytes;
}
 
Example #8
Source Project: amazon-freertos-ble-android-sdk   Author: aws   File: Unsuback.java    License: Apache License 2.0 6 votes vote down vote up
public byte[] encode() {
    byte[] unsubackBytes = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new CborEncoder(baos).encode(new CborBuilder()
                .addMap()
                .put(TYPE_KEY, type)
                .put(MSGID_KEY, msgID)
                .end()
                .build());
        unsubackBytes = baos.toByteArray();
    } catch (CborException e) {
        Log.e(TAG, "Failed to encode.", e);
    }
    return unsubackBytes;
}
 
Example #9
Source Project: amazon-freertos-ble-android-sdk   Author: aws   File: Suback.java    License: Apache License 2.0 6 votes vote down vote up
public byte[] encode() {
    byte[] subackBytes = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new CborEncoder(baos).encode(new CborBuilder()
            .addMap()
            .put(TYPE_KEY, type)
            .put(MSGID_KEY, msgID)
            .put(STATUS_KEY, status)
            .end()
            .build());
        subackBytes = baos.toByteArray();
    } catch (CborException e) {
        Log.e(TAG, "Failed to encode.", e);
    }
    return subackBytes;
}
 
Example #10
Source Project: amazon-freertos-ble-android-sdk   Author: aws   File: Publish.java    License: Apache License 2.0 6 votes vote down vote up
public byte[] encode() {
    byte[] publishBytes = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new CborEncoder(baos).encode(new CborBuilder()
            .addMap()
            .put(TYPE_KEY, type)
            .put(TOPIC_KEY, topic)
            .put(MSGID_KEY, msgID)
            .put(QOS_KEY, qoS)
            .put(PAYLOAD_KEY, payloadBytes)
            .end()
            .build());
        publishBytes = baos.toByteArray();
    } catch (CborException e) {
        Log.e(TAG, "Failed to encode.", e);
    }
    return publishBytes;
}
 
Example #11
Source Project: amazon-freertos-ble-android-sdk   Author: aws   File: Unsubscribe.java    License: Apache License 2.0 6 votes vote down vote up
public byte[] encode() {
    byte[] unsubscribeBytes = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        ArrayBuilder<MapBuilder<CborBuilder>> topicsArray = new CborBuilder()
                .addMap()
                .put(TYPE_KEY, type)
                .putArray(TOPICS_KEY);

        for (String topic : topics) {
            topicsArray = topicsArray.add(topic);
        }

        List<DataItem> cbordata = topicsArray.end().end().build();
        new CborEncoder(baos).encode(cbordata);
        unsubscribeBytes = baos.toByteArray();
    } catch (CborException e) {
        Log.e(TAG, "Failed to encode.", e);
    }
    return unsubscribeBytes;
}
 
Example #12
Source Project: amazon-freertos-ble-android-sdk   Author: aws   File: Connack.java    License: Apache License 2.0 6 votes vote down vote up
public byte[] encode() {
    byte[] connackBytes = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new CborEncoder(baos).encode(new CborBuilder()
            .addMap()
            .put(TYPE_KEY, type)
            .put(STATUS_KEY, status)
            .end()
            .build());
        connackBytes = baos.toByteArray();
    } catch (CborException e) {
        Log.e(TAG, "Failed to encode.", e);
    }
    return connackBytes;
}
 
Example #13
Source Project: amazon-freertos-ble-android-sdk   Author: aws   File: Connect.java    License: Apache License 2.0 6 votes vote down vote up
public byte[] encode() {
    byte[] connectBytes = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new CborEncoder(baos).encode(new CborBuilder()
            .addMap()
            .put(TYPE_KEY, type)
            .put(CLIENTID_KEY, clientID)
            .put(BROKERENDPOINT_KEY, brokerEndpoint)
            .put(CLEANSESSION_KEY, cleanSession)
            .end()
            .build());
        connectBytes = baos.toByteArray();
    } catch (CborException e) {
        Log.e(TAG, "Failed to encode.", e);
    }
    return connectBytes;
}
 
Example #14
Source Project: cbor-java   Author: c-rack   File: MapTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testItemOrderIsPreserved() throws CborException {
    List<DataItem> input = new CborBuilder().addMap().put(1, "v1").put(0, "v2").end().build();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteArrayOutputStream);
    encoder.nonCanonical().encode(input);
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    CborDecoder decoder = new CborDecoder(byteArrayInputStream);
    List<DataItem> output = decoder.decode();
    assertEquals(input, output);
    DataItem dataItem = output.get(0);
    assertEquals(MajorType.MAP, dataItem.getMajorType());
    Map map = (Map) dataItem;
    Collection<DataItem> values = map.getValues();
    Iterator<DataItem> iterator = values.iterator();
    assertEquals(new UnicodeString("v1"), iterator.next());
    assertEquals(new UnicodeString("v2"), iterator.next());
}
 
Example #15
Source Project: cbor-java   Author: c-rack   File: UnsignedIntegerDecoderTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldDecodeBigNumbers() throws CborException {
    BigInteger value = BigInteger.ONE;
    for (int i = 1; i < 64; i++) {
        value = value.shiftLeft(1);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        CborEncoder encoder = new CborEncoder(baos);
        encoder.encode(new CborBuilder().add(value).build());
        byte[] encodedBytes = baos.toByteArray();
        ByteArrayInputStream bais = new ByteArrayInputStream(encodedBytes);
        CborDecoder decoder = new CborDecoder(bais);
        List<DataItem> dataItems = decoder.decode();
        assertNotNull(dataItems);
        assertEquals(1, dataItems.size());
        DataItem dataItem = dataItems.get(0);
        assertTrue(dataItem instanceof UnsignedInteger);
        assertEquals(value, ((UnsignedInteger) dataItem).getValue());
    }
}
 
Example #16
Source Project: cbor-java   Author: c-rack   File: MapBuilderTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void startMapInMap() {
    CborBuilder builder = new CborBuilder();
    List<DataItem> dataItems = builder.addMap().startMap(new ByteString(new byte[] { 0x01 })).put(1, 2).end()
        .startMap(1).end().startMap("asdf").end().end().build();
    Map rootMap = (Map) dataItems.get(0);
    DataItem keys[] = new DataItem[3];
    rootMap.getKeys().toArray(keys);
    assertEquals(keys[0], new ByteString(new byte[] { 0x01 }));
    assertEquals(keys[1], new UnsignedInteger(1));
    assertEquals(keys[2], new UnicodeString("asdf"));

    assertTrue(rootMap.get(keys[0]) instanceof Map);
    assertTrue(rootMap.get(keys[1]) instanceof Map);
    assertTrue(rootMap.get(keys[2]) instanceof Map);
}
 
Example #17
Source Project: sawtooth-sdk-java   Author: hyperledger   File: IntegerKeyHandler.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Helper function to encode the State that will be stored at the address of
 * the name.
 */
public Map.Entry<String, ByteString> encodeState(String address, String name, Long value)
    throws CborException {
  ByteArrayOutputStream boas = new ByteArrayOutputStream();
  new CborEncoder(boas).encode(new CborBuilder()
      .addMap()
      .put(name, value)
      .end()
      .build());

  return new AbstractMap.SimpleEntry<String, ByteString>(
      address,
      ByteString.copyFrom(boas.toByteArray()));
}
 
Example #18
Source Project: amazon-freertos-ble-android-sdk   Author: aws   File: MqttProxyControl.java    License: Apache License 2.0 5 votes vote down vote up
public byte[] encode() {
    byte[] mqttProxyControlBytes = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new CborEncoder(baos).encode(new CborBuilder()
                .addMap()
                .put(PROXYSTATE_KEY, proxyState)
                .end()
                .build());
        mqttProxyControlBytes = baos.toByteArray();
    } catch (CborException e) {
        Log.e(TAG, "Failed to encode.", e);
    }
    return mqttProxyControlBytes;
}
 
Example #19
Source Project: amazon-freertos-ble-android-sdk   Author: aws   File: PingResp.java    License: Apache License 2.0 5 votes vote down vote up
public byte[] encode() {
    byte[] bytes = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new CborEncoder(baos).encode(new CborBuilder()
                .addMap()
                .put(TYPE_KEY, MQTT_MSG_PINGRESP)
                .end()
                .build());
        bytes = baos.toByteArray();
    } catch (CborException e) {
        Log.e(TAG, "Failed to encode.", e);
    }
    return bytes;
}
 
Example #20
Source Project: amazon-freertos-ble-android-sdk   Author: aws   File: Subscribe.java    License: Apache License 2.0 5 votes vote down vote up
public byte[] encode() {
    byte[] subscribeBytes = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        ArrayBuilder<MapBuilder<CborBuilder>> topicsArray = new CborBuilder()
                .addMap()
                .put(TYPE_KEY, type)
                .putArray(TOPICS_KEY);

        for (String topic : topics) {
            topicsArray = topicsArray.add(topic);
        }

        MapBuilder<CborBuilder> map = topicsArray.end();
        ArrayBuilder<MapBuilder<CborBuilder>> qosArray = map.put(MSGID_KEY, msgID)
                .putArray(QOSS_KEY);

        for (int qos : qoSs) {
            qosArray = qosArray.add(qos);
        }
        List<DataItem> cbordata = qosArray.end().end().build();
        new CborEncoder(baos).encode(cbordata);
        subscribeBytes = baos.toByteArray();
    } catch (CborException e) {
        Log.e(TAG, "Failed to encode.", e);
    }
    return subscribeBytes;
}
 
Example #21
Source Project: webauthndemo   Author: google   File: RsaKey.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public byte[] encode() throws CborException {
  ByteArrayOutputStream output = new ByteArrayOutputStream();
  List<DataItem> dataItems =
      new CborBuilder().addMap().put(new UnsignedInteger(KTY_LABEL), new UnsignedInteger(kty))
          .put(new UnsignedInteger(ALG_LABEL), new NegativeInteger(alg.encodeToInt()))
          .put(new NegativeInteger(N_LABEL), new ByteString(n))
          .put(new NegativeInteger(E_LABEL), new ByteString(e)).end().build();
  new CborEncoder(output).encode(dataItems);
  return output.toByteArray();
}
 
Example #22
Source Project: webauthndemo   Author: google   File: EccKey.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public byte[] encode() throws CborException {
  ByteArrayOutputStream output = new ByteArrayOutputStream();
  List<DataItem> dataItems =
      new CborBuilder().addMap().put(new UnsignedInteger(KTY_LABEL), new UnsignedInteger(kty))
          .put(new UnsignedInteger(ALG_LABEL), new NegativeInteger(alg.encodeToInt()))
          .put(new NegativeInteger(CRV_LABEL), new UnsignedInteger(crv))
          .put(new NegativeInteger(X_LABEL), new ByteString(x))
          .put(new NegativeInteger(Y_LABEL), new ByteString(y)).end().build();
  new CborEncoder(output).encode(dataItems);
  return output.toByteArray();
}
 
Example #23
Source Project: webauthndemo   Author: google   File: AttestationObject.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @return Encoded byte array containing AttestationObject data
 * @throws CborException
 */
public byte[] encode() throws CborException {
  ByteArrayOutputStream output = new ByteArrayOutputStream();
  List<DataItem> cbor =
      new CborBuilder().addMap().put("fmt", fmt).put("authData", authData.encode())
          .put(new UnicodeString("attStmt"), attStmt.encode()).end().build();
  new CborEncoder(output).encode(cbor);
  return output.toByteArray();
}
 
Example #24
Source Project: cbor-java   Author: c-rack   File: AbstractEncoderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test(expected = CborException.class)
public void shouldThrowCborExceptionOnUnderlyingIoException() throws CborException {
    final int[] counter = new int[1];
    new CborEncoder(new OutputStream() {

        @Override
        public synchronized void write(int b) throws IOException {
            if (++counter[0] == 3) {
                throw new IOException();
            }
        }

    }).encode(new CborBuilder().startString().add("string").end().build());
}
 
Example #25
Source Project: cbor-java   Author: c-rack   File: AbstractEncoderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test(expected = CborException.class)
public void shouldThrowCborExceptionOnUnderlyingIoException2() throws CborException {
    new CborEncoder(new OutputStream() {

        @Override
        public synchronized void write(int b) throws IOException {
            throw new IOException();
        }

    }).encode(new CborBuilder().startArray().add(1).end().build());
}
 
Example #26
Source Project: cbor-java   Author: c-rack   File: MapEncoderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEncodeMap() throws CborException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    List<DataItem> dataItems = new CborBuilder().addMap().put(1, true).put(".", true).put(3, true).put("..", true)
        .put(2, true).put("...", true).end().build();
    new CborEncoder(byteArrayOutputStream).encode(dataItems);
}
 
Example #27
Source Project: cbor-java   Author: c-rack   File: ByteStringEncoderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEncodeChunkedString() throws CborException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    List<DataItem> dataItems = new CborBuilder().add(new ByteString(null)).add(new ByteString("test".getBytes()))
        .startByteString("test".getBytes()).end().build();
    new CborEncoder(byteArrayOutputStream).encode(dataItems);
}
 
Example #28
Source Project: cbor-java   Author: c-rack   File: Example63Test.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEncode() throws CborException {
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteOutputStream);
    encoder.encode(new CborBuilder().addArray().end().build());
    Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray());
}
 
Example #29
Source Project: cbor-java   Author: c-rack   File: Example64Test.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEncode() throws CborException {
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteOutputStream);
    encoder.encode(new CborBuilder().addArray().add(1).add(2).add(3).end().build());
    Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray());
}
 
Example #30
Source Project: cbor-java   Author: c-rack   File: AbstractNumberTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEncode() throws CborException {
    List<DataItem> dataItems = new CborBuilder().add(value).build();
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteOutputStream);
    encoder.encode(dataItems.get(0));
    Assert.assertArrayEquals(encodedValue, byteOutputStream.toByteArray());
}