UNPKG

rsa-oaep-encryption

Version:

Pure JavaScript implementation of encryption using the RSA-OAEP algorithm without relying on the Web Crypto API.

210 lines (200 loc) 5.09 kB
/** * Constructor for a binary string backed byte buffer. * * @param [b] the bytes to wrap (either encoded as string, one byte per * character, or as an ArrayBuffer or Typed Array). */ declare class ByteStringBuffer { private _constructedStringLength; private data; read: number; constructor(b?: string); private _optimizeConstructedString; /** * Gets the number of bytes in this buffer. * * @return the number of bytes in this buffer. */ length(): number; /** * Puts a byte in this buffer. * * @param b the byte to put. * * @return this buffer. */ putByte(b: number): this; /** * Puts bytes in this buffer. * * @param bytes the bytes (as a binary encoded string) to put. * * @return this buffer. */ putBytes(bytes: string): this; /** * Puts a 32-bit integer in this buffer in big-endian order. * * @param i the 32-bit integer. * * @return this buffer. */ putInt32(i: number): this; /** * Gets a byte from this buffer and advances the read pointer by 1. * * @return the byte. */ getByte(): number; /** * Gets a uint32 from this buffer in big-endian order and advances the read * pointer by 4. * * @return the word. */ getInt32(): number; /** * Gets an n-bit integer from this buffer in big-endian order and advances the * read pointer by ceil(n/8). * * @param n the number of bits in the integer (8, 16, 24, or 32). * * @return the integer. */ getInt(n: 8 | 16 | 24 | 32): number; /** * Reads bytes out as a binary encoded string and clears them from the * buffer. Note that the resulting string is binary encoded (in node.js this * encoding is referred to as `binary`, it is *not* `utf8`). * * @param count the number of bytes to read, undefined or null for all. * * @return a binary encoded string of bytes. */ getBytes(count?: number): string; /** * Gets a binary encoded string of the bytes from this buffer without * modifying the read pointer. * * @param count the number of bytes to get, omit to get all. * * @return a string full of binary encoded characters. */ bytes(count?: number): string; /** * Gets a byte at the given index without modifying the read pointer. * * @param i the byte index. * * @return the byte. */ at(i: number): number; /** * Compacts this buffer. * * @return this buffer. */ compact(): this; /** * Clears this buffer. * * @return this buffer. */ clear(): this; /** * Converts this buffer to a hexadecimal string. * * @return a hexadecimal string. */ toHex(): string; /** * Converts this buffer to an ArrayBuffer. * * @return An ArrayBuffer. */ toArrayBuffer(): ArrayBuffer; } /** * A hash algorithm. */ interface HashAlgorithm { algorithm: string; blockLength: number; digestLength: number; messageLength: number; start(): this; update(msg: string): this; digest(): ByteStringBuffer; } /** * A hash algorithm creator. */ interface HashAlgorithmCreator { create(): HashAlgorithm; } /** * Secure Hash Algorithm with 160-bit digest (SHA-1) implementation. * * @author Dave Longley * * Copyright (c) 2010-2015 Digital Bazaar, Inc. */ /** * SHA-1 algorithm creator. */ declare const sha1: HashAlgorithmCreator; /** * Secure Hash Algorithm with 256-bit digest (SHA-256) implementation. * * See FIPS 180-2 for details. * * @author Dave Longley * * Copyright (c) 2010-2015 Digital Bazaar, Inc. */ /** * SHA-256 algorithm creator. */ declare const sha256: HashAlgorithmCreator; /** * Secure Hash Algorithm with a 1024-bit block size implementation. * * This includes: SHA-512, SHA-384, SHA-512/224, and SHA-512/256. For * SHA-256 (block size 512 bits), see sha256.js. * * See FIPS 180-4 for details. * * @author Dave Longley * * Copyright (c) 2014-2015 Digital Bazaar, Inc. */ /** * SHA-384 algorithm creator. */ declare const sha384: HashAlgorithmCreator; /** * SHA-512 algorithm creator. */ declare const sha512: HashAlgorithmCreator; /** * RSA public key. */ interface RSAPublicKey { /** * Encrypt data using RSA key. * @param data A string to be encrypted. * @param hash Which hash algorithm to use. * @returns Encrypted data as ArrayBuffer. */ encrypt(data: string, hash: HashAlgorithm): ArrayBuffer; } /** * Import a RSA public key from a PEM format string. * Used to encrypt data. * * @param pem The PEM format string. * @returns A function that can be used to encrypt data. */ declare function importPublicKey(pem: string): RSAPublicKey; export { ByteStringBuffer, type HashAlgorithm, type HashAlgorithmCreator, type RSAPublicKey, importPublicKey, sha1, sha256, sha384, sha512 }; //# sourceMappingURL=types.d.ts.map