export async function buildEncryptFixtures()

in modules/integration-browser/src/build_encrypt_fixtures.ts [25:94]


export async function buildEncryptFixtures(
  fixtures: string,
  manifestFile: string,
  keyFile: string,
  testName?: string,
  slice?: string
): Promise<void> {
  const [start = 0, end = 9999] = (slice || '')
    .split(':')
    .map((n) => parseInt(n, 10))
  const { tests, plaintexts }: EncryptManifestList = await getParsedJSON(
    manifestFile
  )
  const { keys }: KeyList = await getParsedJSON(keyFile)

  const plaintextBytes: { [name: string]: string } = {}

  Object.keys(plaintexts).forEach((name) => {
    /* Generate random bites as per spec.
     * See: https://github.com/awslabs/aws-crypto-tools-test-vector-framework/blob/master/features/0003-awses-message-encryption.md#plaintexts
     */
    plaintextBytes[name] = randomBytes(10).toString('base64')
  })

  const testNames = []
  let count = 0

  for (const [name, testInfo] of Object.entries(tests)) {
    count += 1

    if (testName) {
      if (name !== testName) continue
    }

    if (slice) {
      if (start >= count) continue
      if (count > end) continue
    }

    testNames.push(name)

    const {
      plaintext,
      'master-keys': masterKeys,
      algorithm,
      'frame-size': frameLength,
      'encryption-context': encryptionContext,
    } = testInfo

    const keysInfo = masterKeys.map((keyInfo) => {
      const key = keys[keyInfo.key]
      if (!key) throw new Error(`no key for ${name}`)
      return [keyInfo, key] as KeyInfoTuple
    })

    /* I'm expecting that the encrypt function will throw if this is not a supported AlgorithmSuiteIdentifier */
    const suiteId = parseInt(algorithm, 16) as AlgorithmSuiteIdentifier

    const test: EncryptTestVectorInfo = {
      name,
      keysInfo,
      plainTextData: plaintextBytes[plaintext],
      encryptOp: { suiteId, frameLength, encryptionContext },
    }

    writeFileSync(`${fixtures}/${name}.json`, JSON.stringify(test))
  }

  writeFileSync(`${fixtures}/encrypt_tests.json`, JSON.stringify(testNames))
}