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

The following examples show how to use co.nstant.in.cbor.model.NegativeInteger. 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: 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 #2
Source File: RsaKey.java    From webauthndemo with 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 #3
Source File: EccKey.java    From webauthndemo with 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 #4
Source File: CborEncoder.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
/**
 * Encode a single {@link DataItem}.
 *
 * @param dataItem the {@link DataItem} to encode. If null, encoder encodes a
 *                 {@link SimpleValue} NULL value.
 * @throws CborException if {@link DataItem} could not be encoded or there was
 *                       an problem with the {@link OutputStream}.
 */
public void encode(DataItem dataItem) throws CborException {
    if (dataItem == null) {
        dataItem = SimpleValue.NULL;
    }

    if (dataItem.hasTag()) {
        Tag tagDi = dataItem.getTag();
        encode(tagDi);
    }

    switch (dataItem.getMajorType()) {
    case UNSIGNED_INTEGER:
        unsignedIntegerEncoder.encode((UnsignedInteger) dataItem);
        break;
    case NEGATIVE_INTEGER:
        negativeIntegerEncoder.encode((NegativeInteger) dataItem);
        break;
    case BYTE_STRING:
        byteStringEncoder.encode((ByteString) dataItem);
        break;
    case UNICODE_STRING:
        unicodeStringEncoder.encode((UnicodeString) dataItem);
        break;
    case ARRAY:
        arrayEncoder.encode((Array) dataItem);
        break;
    case MAP:
        mapEncoder.encode((Map) dataItem);
        break;
    case SPECIAL:
        specialEncoder.encode((Special) dataItem);
        break;
    case TAG:
        tagEncoder.encode((Tag) dataItem);
        break;
    default:
        throw new CborException("Unknown major type");
    }
}
 
Example #5
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 #6
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 #7
Source File: Example14Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEncode() throws CborException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteArrayOutputStream);
    encoder.encode(new NegativeInteger(new BigInteger("-18446744073709551617")));
    Assert.assertArrayEquals(new byte[] { (byte) 0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
        byteArrayOutputStream.toByteArray());
}
 
Example #8
Source File: EncoderDecoderTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws CborException {
    UnsignedInteger a = new UnsignedInteger(1);
    NegativeInteger x = new NegativeInteger(-2);
    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);
}
 
Example #9
Source File: NegativeIntegerEncoder.java    From cbor-java with Apache License 2.0 4 votes vote down vote up
@Override
public void encode(NegativeInteger dataItem) throws CborException {
    encodeTypeAndLength(MajorType.NEGATIVE_INTEGER, MINUS_ONE.subtract(dataItem.getValue()).abs());
}
 
Example #10
Source File: NegativeIntegerDecoder.java    From cbor-java with Apache License 2.0 4 votes vote down vote up
@Override
public NegativeInteger decode(int initialByte) throws CborException {
    return new NegativeInteger(MINUS_ONE.subtract(getLengthAsBigInteger(initialByte)));
}