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

The following examples show how to use co.nstant.in.cbor.CborEncoder. 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
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 #3
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 #4
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 #5
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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
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 #13
Source Project: cbor-java   Author: c-rack   File: LanguageTaggedStringDecoderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test(expected = CborException.class)
public void shouldThrowException() throws CborException {
    List<DataItem> items = new CborBuilder().addTag(38).build();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(baos);
    encoder.encode(items);
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    CborDecoder decoder = new CborDecoder(bais);
    decoder.decode();
}
 
Example #14
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 #15
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 #16
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 #17
Source Project: cbor-java   Author: c-rack   File: LanguageTaggedStringDecoderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testDecoding() throws CborException {
    List<DataItem> items = new CborBuilder().addTag(38).addArray().add("en").add("string").end().build();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(baos);
    encoder.encode(items);
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    CborDecoder decoder = new CborDecoder(bais);
    DataItem item = decoder.decodeNext();
    assertEquals(new LanguageTaggedString(new UnicodeString("en"), new UnicodeString("string")), item);
}
 
Example #18
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 #19
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 #20
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 #21
Source Project: cbor-java   Author: c-rack   File: CborDecoderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecodeTaggedRationalNumber() throws CborException {
    List<DataItem> items = new CborBuilder().addTag(1).addTag(30).addArray().add(1).add(2).end().build();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(baos);
    encoder.encode(items);
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    CborDecoder decoder = new CborDecoder(bais);

    RationalNumber expected = new RationalNumber(new UnsignedInteger(1), new UnsignedInteger(2));
    expected.getTag().setTag(new Tag(1));
    assertEquals(expected, decoder.decodeNext());
}
 
Example #22
Source Project: cbor-java   Author: c-rack   File: CborDecoderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecodeRationalNumber() throws CborException {
    List<DataItem> items = new CborBuilder().addTag(30).addArray().add(1).add(2).end().build();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(baos);
    encoder.encode(items);
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    CborDecoder decoder = new CborDecoder(bais);
    assertEquals(new RationalNumber(new UnsignedInteger(1), new UnsignedInteger(2)), decoder.decodeNext());
}
 
Example #23
Source Project: cbor-java   Author: c-rack   File: AbstractDoublePrecisionFloatTest.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());
}
 
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: SimpleValueDecoderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecodeBoolean() throws CborException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteArrayOutputStream);
    encoder.encode(SimpleValue.TRUE);
    encoder.encode(SimpleValue.FALSE);
    byte[] encodedBytes = byteArrayOutputStream.toByteArray();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(encodedBytes);
    List<DataItem> dataItems = new CborDecoder(byteArrayInputStream).decode();
    int result = 0;
    int position = 1;
    for (DataItem dataItem : dataItems) {
        position++;
        switch (dataItem.getMajorType()) {
        case SPECIAL:
            Special special = (Special) dataItem;
            switch (special.getSpecialType()) {
            case SIMPLE_VALUE:
                SimpleValue simpleValue = (SimpleValue) special;
                switch (simpleValue.getSimpleValueType()) {
                case FALSE:
                    result += position * 2;
                    break;
                case TRUE:
                    result += position * 3;
                    break;
                default:
                    break;
                }
                break;
            default:
                break;
            }
            break;
        default:
            break;
        }
    }
    assertEquals(12, result);
}
 
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: Example72Test.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(VALUE);
    Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray());
}
 
Example #29
Source Project: cbor-java   Author: c-rack   File: Example78Test.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(VALUE);
    Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray());
}
 
Example #30
Source Project: cbor-java   Author: c-rack   File: Example41Test.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(SimpleValue.FALSE);
    Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray());
}