How to use the @aws-crypto/crc32.Crc32 function in @aws-crypto/crc32

To help you get started, we’ve selected a few @aws-crypto/crc32 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github aws / aws-sdk-js-v3 / packages / eventstream-marshaller / src / EventStreamMarshaller.ts View on Github external
marshall({ headers: rawHeaders, body }: Message): Uint8Array {
    const headers = this.headerMarshaller.format(rawHeaders);
    const length = headers.byteLength + body.byteLength + 16;

    const out = new Uint8Array(length);
    const view = new DataView(out.buffer, out.byteOffset, out.byteLength);
    const checksum = new Crc32();

    // Format message
    view.setUint32(0, length, false);
    view.setUint32(4, headers.byteLength, false);
    view.setUint32(8, checksum.update(out.subarray(0, 8)).digest(), false);
    out.set(headers, 12);
    out.set(body, headers.byteLength + 12);

    // Write trailing message checksum
    view.setUint32(
      length - 4,
      checksum.update(out.subarray(8, length - 4)).digest(),
      false
    );

    return out;
github aws / aws-sdk-js-v3 / packages / eventstream-marshaller / src / splitMessage.ts View on Github external
const messageLength = view.getUint32(0, false);

  if (byteLength !== messageLength) {
    throw new Error(
      "Reported message length does not match received message length"
    );
  }

  const headerLength = view.getUint32(PRELUDE_MEMBER_LENGTH, false);
  const expectedPreludeChecksum = view.getUint32(PRELUDE_LENGTH, false);
  const expectedMessageChecksum = view.getUint32(
    byteLength - CHECKSUM_LENGTH,
    false
  );

  const checksummer = new Crc32().update(
    new Uint8Array(buffer, byteOffset, PRELUDE_LENGTH)
  );
  if (expectedPreludeChecksum !== checksummer.digest()) {
    throw new Error(
      `The prelude checksum specified in the message (${expectedPreludeChecksum}) does not match the calculated CRC32 checksum (${checksummer.digest()})`
    );
  }

  checksummer.update(
    new Uint8Array(
      buffer,
      byteOffset + PRELUDE_LENGTH,
      byteLength - (PRELUDE_LENGTH + CHECKSUM_LENGTH)
    )
  );
  if (expectedMessageChecksum !== checksummer.digest()) {

@aws-crypto/crc32

Pure JS implementation of CRC32 https://en.wikipedia.org/wiki/Cyclic_redundancy_check

Apache-2.0
Latest version published 1 year ago

Package Health Score

88 / 100
Full package analysis

Popular @aws-crypto/crc32 functions

Similar packages