co.nstant.in.cbor.model.DataItem Java Examples

The following examples show how to use co.nstant.in.cbor.model.DataItem. 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
Source File: Unsubscribe.java    From amazon-freertos-ble-android-sdk with 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 #2
Source File: EncoderDecoderTest.java    From cbor-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testTagging() throws CborException {
    UnsignedInteger a = new UnsignedInteger(1);
    NegativeInteger x = new NegativeInteger(-2);

    a.setTag(1);

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteArrayOutputStream);
    encoder.encode(a);
    encoder.encode(x);
    byte[] bytes = byteArrayOutputStream.toByteArray();
    DataItem object = CborDecoder.decode(bytes).get(0);
    Assert.assertEquals(a, object);
    Assert.assertTrue(object.hasTag());
    Assert.assertEquals(1L, object.getTag().getValue());
}
 
Example #3
Source File: AuthenticatorData.java    From webauthndemo with Apache License 2.0 6 votes vote down vote up
/**
 * Parse Attestation extensions
 * 
 * @return extension map
 */
private HashMap<String, AttestationExtension> parseExtensions(byte[] extensions) {
  HashMap<String, AttestationExtension> extensionMap = new HashMap<>();

  try {
    List<DataItem> dataItems = CborDecoder.decode(extensions);

    if (dataItems.size() < 1 || !(dataItems.get(0) instanceof Map)) {
      return extensionMap;
    }
    Map map = (Map) dataItems.get(0);

    for (DataItem data : map.getKeys()) {
      if (data instanceof UnicodeString) {
        if (((UnicodeString) data).getString().equals(CableRegistrationData.KEY)) {
          CableRegistrationData decodedCableData = CableRegistrationData.parseFromCbor(map.get(data));
          extensionMap.put(CableRegistrationData.KEY, decodedCableData);
        }
      }
    }
  } catch (CborException e) {
    e.printStackTrace();
  }
  return extensionMap;
}
 
Example #4
Source File: PackedAttestationStatement.java    From webauthndemo with Apache License 2.0 6 votes vote down vote up
@Override
DataItem encode() throws CborException {
  Map result = new Map();
  if (attestnCert != null) {
    Array x5c = new Array();
    x5c.add(new ByteString(attestnCert));
    for (byte[] cert : this.caCert) {
      x5c.add(new ByteString(cert));
    }
    result.put(new UnicodeString("x5c"), x5c);
  }
  if (ecdaaKeyId != null) {
    result.put(new UnicodeString("ecdaaKeyId"), new ByteString(ecdaaKeyId));
  }
  result.put(new UnicodeString("sig"), new ByteString(sig));
  result.put(new UnicodeString("alg"), new UnicodeString(alg.toString()));

  return result;
}
 
Example #5
Source File: MapBuilderTest.java    From cbor-java with 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 #6
Source File: MapEncoder.java    From cbor-java with Apache License 2.0 6 votes vote down vote up
@Override
public void encode(Map map) throws CborException {
    Collection<DataItem> keys = map.getKeys();

    if (map.isChunked()) {
        encodeTypeChunked(MajorType.MAP);
    } else {
        encodeTypeAndLength(MajorType.MAP, keys.size());
    }

    if (keys.isEmpty()) {
        return;
    }

    if (map.isChunked()) {
        encodeNonCanonical(map);
        encoder.encode(SimpleValue.BREAK);
    } else {
        if (encoder.isCanonical()) {
            encodeCanonical(map);
        } else {
            encodeNonCanonical(map);
        }
    }
}
 
Example #7
Source File: AndroidSafetyNetAttestationStatementTest.java    From webauthndemo with Apache License 2.0 6 votes vote down vote up
/**
 * Test method for
 * {@link com.google.webauthn.gaedemo.objects.AndroidSafetyNetAttestationStatement#encode()} and
 * {@link com.google.webauthn.gaedemo.objects.AndroidSafetyNetAttestationStatement#decode(co.nstant.in.cbor.model.DataItem)}.
 */
@Ignore
@Test
public void testEncode() {
  SecureRandom random = new SecureRandom();
  AndroidSafetyNetAttestationStatement attStmt = new AndroidSafetyNetAttestationStatement();
  attStmt.ver = "10";
  attStmt.response = new byte[20];
  random.nextBytes(attStmt.response);

  try {
    DataItem encoded = attStmt.encode();
    AndroidSafetyNetAttestationStatement decoded =
        AndroidSafetyNetAttestationStatement.decode(encoded);
    assertEquals(decoded, attStmt);
  } catch (CborException | ResponseException e) {
    fail(e.getMessage());
  }
}
 
Example #8
Source File: ByteStringDecoder.java    From cbor-java with Apache License 2.0 6 votes vote down vote up
private ByteString decodeInfinitiveLength() throws CborException {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    for (;;) {
        DataItem dataItem = decoder.decodeNext();
        if (dataItem == null) {
            throw new CborException("Unexpected end of stream");
        }
        MajorType majorType = dataItem.getMajorType();
        if (Special.BREAK.equals(dataItem)) {
            break;
        } else if (majorType == MajorType.BYTE_STRING) {
            ByteString byteString = (ByteString) dataItem;
            byte[] byteArray = byteString.getBytes();
            if (byteArray != null) {
                bytes.write(byteArray, 0, byteArray.length);
            }
        } else {
            throw new CborException("Unexpected major type " + majorType);
        }
    }
    return new ByteString(bytes.toByteArray());
}
 
Example #9
Source File: AndroidSafetyNetAttestationStatement.java    From webauthndemo with Apache License 2.0 6 votes vote down vote up
/**
 * Decodes a cbor representation of an AndroidSafetyNetAttestationStatement into the object
 * representation
 *
 * @param attStmt Cbor DataItem representation of the attestation statement to decode
 * @return Decoded AndroidSafetyNetAttestationStatement
 * @throws ResponseException Input was not a valid AndroidSafetyNetAttestationStatement DataItem
 */
public static AndroidSafetyNetAttestationStatement decode(DataItem attStmt)
    throws ResponseException {
  AndroidSafetyNetAttestationStatement result = new AndroidSafetyNetAttestationStatement();
  Map given = (Map) attStmt;
  for (DataItem data : given.getKeys()) {
    if (data instanceof UnicodeString) {
      if (((UnicodeString) data).getString().equals("ver")) {
        UnicodeString version = (UnicodeString) given.get(data);
        result.ver = version.getString();
      } else if (((UnicodeString) data).getString().equals("response")) {
        result.response = ((ByteString) (given.get(data))).getBytes();
      }
    }
  }
  if (result.response == null || result.ver == null)
    throw new ResponseException("Invalid JWT Cbor");
  return result;
}
 
Example #10
Source File: Example44Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    DataItem dataItem = decoder.decodeNext();
    Assert.assertTrue(dataItem instanceof SimpleValue);
    SimpleValue simpleValue = (SimpleValue) dataItem;
    Assert.assertEquals(SimpleValue.UNDEFINED, simpleValue);
}
 
Example #11
Source File: CborDecoderTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test(expected = CborException.class)
public void shouldThrowOnRationalNumberDecode2() throws CborException {
    List<DataItem> items = new CborBuilder().addTag(30).addArray().add(true).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);
    decoder.decode();
}
 
Example #12
Source File: Example63Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    DataItem dataItem = decoder.decodeNext();
    Assert.assertTrue(dataItem instanceof Array);
    Array array = (Array) dataItem;
    Assert.assertTrue(array.getDataItems().isEmpty());
}
 
Example #13
Source File: CborDecoderTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecodeTaggedTags() throws CborException {
    DataItem decoded = CborDecoder.decode(new byte[] { (byte) 0xC1, (byte) 0xC2, 0x02 }).get(0);

    Tag outer = new Tag(1);
    Tag inner = new Tag(2);
    UnsignedInteger expected = new UnsignedInteger(2);
    inner.setTag(outer);
    expected.setTag(inner);

    assertEquals(expected, decoded);
}
 
Example #14
Source File: AbstractDoublePrecisionFloatTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(encodedValue);
    CborDecoder decoder = new CborDecoder(inputStream);
    DataItem dataItem = decoder.decodeNext();
    Assert.assertTrue(dataItem instanceof DoublePrecisionFloat);
    DoublePrecisionFloat doublePrecisionFloat = (DoublePrecisionFloat) dataItem;
    Assert.assertEquals(value, doublePrecisionFloat.getValue(), 0);
}
 
Example #15
Source File: Example41Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    DataItem dataItem = decoder.decodeNext();
    Assert.assertTrue(dataItem instanceof SimpleValue);
    SimpleValue simpleValue = (SimpleValue) dataItem;
    Assert.assertEquals(SimpleValue.FALSE, simpleValue);
}
 
Example #16
Source File: MapDecoderTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldUseLastOfDuplicateKeysByDefault() throws CborException {
    byte[] bytes = new byte[] { (byte) 0xa2, 0x01, 0x01, 0x01, 0x02 };
    List<DataItem> decoded = CborDecoder.decode(bytes);
    Map map = (Map) decoded.get(0);
    assertEquals(map.getKeys().size(), 1);
    assertEquals(map.get(new UnsignedInteger(1)), new UnsignedInteger(2));
}
 
Example #17
Source File: AbstractBuilder.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
protected DataItem convert(long value) {
    if (value >= 0) {
        return new UnsignedInteger(value);
    } else {
        return new NegativeInteger(value);
    }
}
 
Example #18
Source File: Example49Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    List<DataItem> dataItems = decoder.decode();
    Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray());
}
 
Example #19
Source File: Example47Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    DataItem dataItem = decoder.decodeNext();
    Assert.assertTrue(dataItem instanceof SimpleValue);
    SimpleValue simpleValue = (SimpleValue) dataItem;
    Assert.assertEquals(VALUE, simpleValue);
}
 
Example #20
Source File: Example48Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    List<DataItem> dataItems = decoder.decode();
    Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray());
}
 
Example #21
Source File: AbstractBuilder.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
protected DataItem convert(boolean value) {
    if (value) {
        return SimpleValue.TRUE;
    } else {
        return SimpleValue.FALSE;
    }
}
 
Example #22
Source File: AbstractBuilder.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
protected DataItem convert(BigInteger value) {
    if (value.signum() == -1) {
        return new NegativeInteger(value);
    } else {
        return new UnsignedInteger(value);
    }
}
 
Example #23
Source File: ArrayBuilder.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
public ArrayBuilder<T> tagged(long tag) {
    DataItem item = array.peekLast();
    if (item == null) {
        throw new IllegalStateException("Can't add a tag before adding an item");
    }
    item.getOuterTaggable().setTag(tag);
    return this;
}
 
Example #24
Source File: Example52Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    List<DataItem> dataItems = decoder.decode();
    Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray());
}
 
Example #25
Source File: ByteStringDecoderTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecodeByteString1M() throws CborException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(baos);
    encoder.encode(new CborBuilder().add(new byte[1024 * 1024]).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());
}
 
Example #26
Source File: ArrayBuilderTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldAddByteArray() {
    CborBuilder builder = new CborBuilder();
    List<DataItem> dataItems = builder.addArray().add(new byte[] { 0x0 }).end().build();
    assertEquals(1, dataItems.size());
    assertTrue(dataItems.get(0) instanceof Array);
    Array array = (Array) dataItems.get(0);
    assertEquals(1, array.getDataItems().size());
    assertTrue(array.getDataItems().get(0) instanceof ByteString);
}
 
Example #27
Source File: Example68Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    List<DataItem> dataItems = decoder.decode();
    Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray());
}
 
Example #28
Source File: ArrayDecoder.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
private Array decodeFixedLength(long length) throws CborException {
    Array array = new Array(getPreallocationSize(length));
    for (long i = 0; i < length; i++) {
        DataItem dataItem = decoder.decodeNext();
        if (dataItem == null) {
            throw new CborException("Unexpected end of stream");
        }
        array.add(dataItem);
    }
    return array;
}
 
Example #29
Source File: LanguageTaggedStringDecoderTest.java    From cbor-java with 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 #30
Source File: AbstractNumberTest.java    From cbor-java with 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());
}