Options
All
  • Public
  • Public/Protected
  • All
Menu

@webcrypto/tools

Index

Type aliases

CryptoAlgorithm: AlgorithmIdentifier | RsaOaepParams | AesCtrParams | AesCbcParams | AesGcmParams

Params for Encrypt / Decrypt Algorithms at Web Crypto API

CryptoKeyUsage: "encrypt" | "decrypt" | "deriveKey" | "deriveBits" | "wrapKey" | "sign" | "verify" | "unwrapKey"

Possible uses of Crypto Keys

DeriveAlgorithm: AlgorithmIdentifier | EcdhKeyDeriveParams | HkdfParams | Pbkdf2Params

Derive Key Algorithms at at Web Crypto API

DerivedAlgorithmFor: AlgorithmIdentifier | AesDerivedKeyParams | HmacImportParams | HkdfParams | Pbkdf2Params

Derive Algorithms Params for Web Crypto API

ImportAlgorithm: AlgorithmIdentifier | RsaHashedImportParams | EcKeyImportParams | HmacImportParams | AesKeyAlgorithm

Import Key Algorithms at Web Crypto API

Variables

PBKDF2_ITERATIONS_DEFAULT: number = 50000

Default number of iterations used with PBKDF2 algorithm

Functions

  • decode(data: string | BufferSource): string
  • Decode a ArrayBuffer value to a string. If the given value is already a string, then the value will be returned without any transformation.

    Parameters

    • data: string | BufferSource

      Value to be decoded.

    Returns string

    The transformed given value as a string.

  • decryptValue(data: BufferSource, cryptoKey: CryptoKey, nonceOrAlgorithm: BufferSource | CryptoAlgorithm): Promise<ArrayBuffer>
  • Decrypt a value with the given Crypto Key and Algorithm

    Parameters

    • data: BufferSource

      Value to be encrypted.

    • cryptoKey: CryptoKey

      The Crypto Key used in encryption.

    • nonceOrAlgorithm: BufferSource | CryptoAlgorithm

      The nonce used for AES encryption or the custom algorithm.

    Returns Promise<ArrayBuffer>

    A promise with the decrypt value

  • Derives a base Crypto Key to new one that can be used in encrypt / decrypt algorithms or any other possible uses in CryptoKeyUsage.

    Parameters

    • cryptoBaseKey: CryptoKey

      The base Crypto Key to be derive.

    • salt: BufferSource

      The salt value to be used with the default PBKDF2 derive algorithm.

    • Optional iterations: number

      The number of iterations to be used with the default PBKDF2 derive algorithm. Default value: PBKDF2_ITERATIONS_DEFAULT.

    • Optional keyUsages: CryptoKeyUsage[]

      The new uses of the new derive Crypto Key. Default value: ['encrypt', 'decrypt'].

    Returns Promise<CryptoKey>

    A promise with the derived Crypto Key for other uses.

  • Derives a base Crypto Key to new one that can be used in encrypt / decrypt algorithms or any other possible uses in CryptoKeyUsage.

    Parameters

    • cryptoBaseKey: CryptoKey

      The base Crypto Key to be derive.

    • salt: BufferSource

      The salt value to be used with the default PBKDF2 derive algorithm.

    • Optional algorithmFor: DerivedAlgorithmFor

      The algorithm where the derived Crypto Key will be used. Default value: { name: 'AES-GCM', length: 256 }.

    • Optional keyUsages: CryptoKeyUsage[]

      The new uses of the new derive Crypto Key. Default value: ['encrypt', 'decrypt'].

    Returns Promise<CryptoKey>

    A promise with the derived Crypto Key for other uses.

  • Derives a base Crypto Key to new one that can be used in encrypt / decrypt algorithms or any other possible uses in CryptoKeyUsage.

    Parameters

    • cryptoBaseKey: CryptoKey

      The base Crypto Key to be derive.

    • deriveAlgorithm: DeriveAlgorithm

      The algorithm to be used when deriving the Crypto Key.

    • Optional algorithmFor: DerivedAlgorithmFor

      The algorithm where the derived Crypto Key will be used. Default value: { name: 'AES-GCM', length: 256 }.

    • Optional keyUsages: CryptoKeyUsage[]

      The new uses of the new derive Crypto Key. Default value: ['encrypt', 'decrypt'].

    Returns Promise<CryptoKey>

    A promise with the derived Crypto Key for other uses.

  • encode(data: string | BufferSource): BufferSource
  • Encode a string value to a Typed Array as Uint8Array. If the given value is already a Typed Array, then the value will be returned without any transformation.

    Parameters

    • data: string | BufferSource

      Value to be encoded.

    Returns BufferSource

    The transformed given value as a Typed Array.

  • encryptValue(data: string | BufferSource, cryptoKey: CryptoKey, algorithm?: CryptoAlgorithm): Promise<[ArrayBuffer, BufferSource | null]>
  • Encrypt a value with the given Crypto Key and Algorithm

    Parameters

    • data: string | BufferSource

      Value to be encrypted.

    • cryptoKey: CryptoKey

      The Crypto Key to be used in encryption.

    • algorithm: CryptoAlgorithm = ...

      The algorithm to be used in encryption. Default to AES-GCM.

    Returns Promise<[ArrayBuffer, BufferSource | null]>

    A promise with the encrypted value and the used nonce, if used with the encryption algorithm.

  • generateBaseCryptoKey(rawKey: string | BufferSource | JsonWebKey, algorithm?: ImportAlgorithm, keyUsages?: KeyUsage[], format?: KeyFormat): Promise<CryptoKey>
  • Creates a base Crypto Key from the original raw key, by default this base key should just be used to protect the original key to be discovery, and should not be used directly to any encrypt / decrypt algorithm. The generated base crypto key should be used just to derive new ones, that then will be used to encrypt / decrypt algorithms.

    Parameters

    • rawKey: string | BufferSource | JsonWebKey

      The original key to start the encrypt process.

    • algorithm: ImportAlgorithm = 'PBKDF2'

      The algorithm used to import the key.

    • keyUsages: KeyUsage[] = ...

      The uses for the generated Crypto Key.

    • format: KeyFormat = 'raw'

      Input format for the raw key.

    Returns Promise<CryptoKey>

    A promise with the base Crypto Key.

  • generateHash(data: string | BufferSource, algorithm?: string | Algorithm): Promise<ArrayBuffer>
  • Generates a hash value for the given value.

    Parameters

    • data: string | BufferSource

      Seed value to generate a hash.

    • algorithm: string | Algorithm = 'SHA-256'

      The algorithm to be used when generating the hash.

    Returns Promise<ArrayBuffer>

    A promise containing the hash value.

  • generateNonce(byteSize?: number): Uint8Array
  • Generates random value to be used as nonce with encryption algorithms.

    Parameters

    • byteSize: number = 16

      The byte size of the generated random value.

    Returns Uint8Array

    The random value.

  • generateRandomValues(byteSize?: number): Uint8Array
  • Generates random value as a typed array of Uint8Array.

    Parameters

    • byteSize: number = 8

      The byte size of the generated random value.

    Returns Uint8Array

    The random value.

  • generateSalt(byteSize?: number): Uint8Array
  • Generates random value to be used as salt with encryption algorithms.

    Parameters

    • byteSize: number = 8

      The byte size of the generated random value.

    Returns Uint8Array

    The random value.

  • getCryptoObject(): Crypto
  • Returns the crypto object depending on browser support. IE11 has support for the Crypto API, but it is in a different global scope.

    Returns Crypto

    The Crypto object.

  • isTypedArray(data: unknown): data is BufferSource
  • Type Guard to Typed Array.

    Parameters

    • data: unknown

      Any data to be checked.

    Returns data is BufferSource

    Verify if the given data is a Typed Array.

Generated using TypeDoc