diff --git a/openpgp/openpgp-tests.ts b/openpgp/openpgp-tests.ts new file mode 100644 index 000000000..464bc77cd --- /dev/null +++ b/openpgp/openpgp-tests.ts @@ -0,0 +1,128 @@ +/// + +// Open PGP Sample codes + +var options: openpgp.KeyOptions = { + numBits: 2048, + userId: 'Jon Smith ', + passphrase: 'super long and hard to guess secret' +}; + +openpgp.generateKeyPair(options).then(function (keypair) { + // success + var privkey = keypair.privateKeyArmored; + var pubkey = keypair.publicKeyArmored; +}).catch(function (error) { + // failure +}); + + +var spubkey = '-----BEGIN PGP PUBLIC KEY BLOCK ... END PGP PUBLIC KEY BLOCK-----'; +var publicKey = openpgp.key.readArmored(spubkey); + +openpgp.encryptMessage(publicKey.keys, 'Hello, World!').then(function (pgpMessage) { + // success +}).catch(function (error) { + // failure +}); + + + +var sprivkey = '-----BEGIN PGP PRIVATE KEY BLOCK ... END PGP PRIVATE KEY BLOCK-----'; +var privateKey = openpgp.key.readArmored(sprivkey).keys[0]; +privateKey.decrypt('passphrase'); + +var pgpMessageStr = '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'; +var pgpMessage = openpgp.message.readArmored(pgpMessageStr); + +openpgp.decryptMessage(privateKey, pgpMessage).then(function (plaintext) { + // success +}).catch(function (error) { + // failure +}); + + +// Open PGP Tests + + +var keyoptions: openpgp.KeyOptions; +var key= openpgp.key.generate(keyoptions); +var keys: Array; +var message = openpgp.message.readArmored(""); +var cleartextmessage = openpgp.cleartext.readArmored(""); +var mpi: openpgp.crypto.Mpi; +var mpis: Array; + + +openpgp.decryptAndVerifyMessage(key, key, "").then(); +openpgp.decryptAndVerifyMessage(key, keys, "").then(); + +openpgp.decryptMessage(key, message).then(); + +openpgp.encryptMessage(key, "").then(); +openpgp.encryptMessage(keys, "").then(); + +openpgp.generateKeyPair(keyoptions).then(function (keypair) { + key = keypair.key; +}); + +openpgp.signAndEncryptMessage(key, key, "").then(); +openpgp.signAndEncryptMessage(keys, key, "").then(); + +openpgp.signClearMessage(key, "").then(); +openpgp.signClearMessage(keys, "").then(); + +openpgp.verifyClearSignedMessage(key, cleartextmessage); +openpgp.verifyClearSignedMessage(keys, cleartextmessage); + +openpgp.armor.armor(openpgp.enums.armor.message, {}, 0, 1); +openpgp.armor.dearmor(""); + +openpgp.cleartext.readArmored(""); + +openpgp.crypto.generateSessionKey(openpgp.enums.symmetric.aes128); +openpgp.crypto.getPrefixRandom(openpgp.enums.symmetric.aes128); +openpgp.crypto.getPrivateMpiCount(openpgp.enums.symmetric.aes128); +openpgp.crypto.publicKeyDecrypt(openpgp.enums.publicKey.rsa_encrypt, mpis, mpis, mpi); +openpgp.crypto.publicKeyEncrypt(openpgp.enums.publicKey.rsa_encrypt, mpis, mpi); + +openpgp.crypto.cfb.decrypt("", "", "", true); +openpgp.crypto.cfb.encrypt("", "", "", "", true); +openpgp.crypto.cfb.mdc({}, "", ""); + +openpgp.crypto.hash.digest(openpgp.enums.hash.md5, ""); +openpgp.crypto.hash.getHashByteLength(openpgp.enums.hash.md5); + +openpgp.crypto.random.getRandomBigInteger(0); +openpgp.crypto.random.getRandomBytes(0); +openpgp.crypto.random.getRandomValues(openpgp.util.str2Uint8Array("")); +openpgp.crypto.random.getSecureRandom(0, 1); + +openpgp.crypto.signature.sign(openpgp.enums.hash.md5, openpgp.enums.publicKey.rsa_encrypt, mpis, mpis, ""); +openpgp.crypto.signature.verify(openpgp.enums.publicKey.rsa_encrypt, openpgp.enums.hash.md5, mpis, mpis, ""); + +openpgp.key.generate(keyoptions); +openpgp.key.readArmored(""); + +openpgp.message.fromBinary(""); +openpgp.message.fromText(""); +openpgp.message.readArmored(""); + +openpgp.packet.fromStructuredClone({}); +openpgp.packet.newPacketFromTag(""); + +openpgp.util.bin2str([0, 1]); +openpgp.util.calc_checksum(""); +openpgp.util.decode_utf8(""); +openpgp.util.encode_utf8(""); +openpgp.util.get_hashAlgorithmString(); +openpgp.util.getWebCrypto(); +openpgp.util.hex2bin(""); +openpgp.util.hexidump(""); +openpgp.util.hexstrdump(""); +openpgp.util.print_debug(""); +openpgp.util.print_debug_hexstr_dump(""); +openpgp.util.shiftRight("", 1); +openpgp.util.str2bin(""); +openpgp.util.str2Uint8Array(""); +openpgp.util.Uint8Array2str(openpgp.util.str2Uint8Array("")); \ No newline at end of file diff --git a/openpgp/openpgp.d.ts b/openpgp/openpgp.d.ts new file mode 100644 index 000000000..0ae8445a2 --- /dev/null +++ b/openpgp/openpgp.d.ts @@ -0,0 +1,552 @@ +// Type definitions for openpgpjs +// Project: http://openpgpjs.org/ +// Definitions by: Guillaume Lacasa +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare module openpgp { + + interface KeyPair { + key: key.Key, + privateKeyArmored: string, + publicKeyArmored: string + } + + interface KeyOptions { + keyType?: enums.publicKey, + numBits: number, + userId: string, + passphrase: string, + unlocked?: boolean + } + + interface Keyid { + bytes: String, + } + + interface Signature { + keyid: Keyid, + valid: boolean + } + + interface VerifiedMessage { + text: String, + signatures: Array + } + + /** Decrypts message and verifies signatures + + @param privateKey private key with decrypted secret key data + @param publicKeys array of keys to verify signatures + @param msg the message object with signed and encrypted data + */ + function decryptAndVerifyMessage(privateKey: key.Key, publicKeys: Array, msg: String): Promise; + /** Decrypts message and verifies signatures + + @param privateKey private key with decrypted secret key data + @param publicKey single key to verify signatures + @param msg the message object with signed and encrypted data + */ + function decryptAndVerifyMessage(privateKey: key.Key, publicKey: key.Key, msg: String): Promise; + + /** Decrypts message + + @param privateKey private key with decrypted secret key data + @param msg the message object with the encrypted data + */ + function decryptMessage(privateKey: key.Key, msg: message.Message): Promise; + + /** Encrypts message text with keys + @param keys array of keys used to encrypt the message + @param text message as native JavaScript string + @returns encrypted ASCII armored message + */ + function encryptMessage(keys: Array, message: string): Promise; + /** Encrypts message text with keys + + @param single key used to encrypt the message + @param text message as native JavaScript string + */ + function encryptMessage(key: key.Key, message: string): Promise; + + /** Generates a new OpenPGP key pair. Currently only supports RSA keys. Primary and subkey will be of same type. + @param options + */ + function generateKeyPair(options: KeyOptions): Promise; + + /** Signs message text and encrypts it + + @param publicKeys array of keys used to encrypt the message + @param privateKey private key with decrypted secret key data for signing + @param text private key with decrypted secret key data for signing + */ + function signAndEncryptMessage(publicKeys: Array, privateKey: key.Key, text: String): Promise; + /** Signs message text and encrypts it + + @param publicKeys single key used to encrypt the message + @param privateKey private key with decrypted secret key data for signing + @param text private key with decrypted secret key data for signing + */ + function signAndEncryptMessage(publicKey: key.Key, privateKey: key.Key, text: String): Promise; + + /** Signs a cleartext message + + @param privateKeys array of keys with decrypted secret key data to sign cleartext + @param text cleartext + */ + function signClearMessage(privateKeys: Array, text: String): Promise; + /** Signs a cleartext message + + @param privateKeys single key with decrypted secret key data to sign cleartext + @param text cleartext + */ + function signClearMessage(privateKey: key.Key, text: String): Promise; + + /** Verifies signatures of cleartext signed message + + @param publicKeys array of keys to verify signatures + @param msg cleartext message object with signatures + */ + function verifyClearSignedMessage(publicKeys: Array, msg: cleartext.CleartextMessage): Promise; + /** Verifies signatures of cleartext signed message + + @param publicKeys single key to verify signatures + @param msg cleartext message object with signatures + */ + function verifyClearSignedMessage(publicKey: key.Key, msg: cleartext.CleartextMessage): Promise; +} + +declare module openpgp.armor { + + /** Armor an OpenPGP binary packet block + + @param messagetype type of the message + @param body + @param partindex + @param parttotal + */ + function armor(messagetype: enums.armor, body: Object, partindex: Number, parttotal: Number): String; + + /** DeArmor an OpenPGP armored message; verify the checksum and return the encoded bytes + + @param text OpenPGP armored message + */ + function dearmor(text: String): Object; +} + +declare module openpgp.cleartext { + + /** Class that represents an OpenPGP cleartext signed message. + */ + interface CleartextMessage { + /** Returns ASCII armored text of cleartext signed message + */ + armor(): String; + + /** Returns the key IDs of the keys that signed the cleartext message + */ + getSigningKeyIds(): Array; + + /** Get cleartext + */ + getText(): String; + + /** Sign the cleartext message + @param privateKeys private keys with decrypted secret key data for signing + */ + sign(privateKeys: Array): void; + + /** Verify signatures of cleartext signed message + @param keys array of keys to verify signatures + */ + verify(keys: Array): Array; + } + + function readArmored(armoredText: String): CleartextMessage; +} + +declare module openpgp.config { + var prefer_hash_algorithm: enums.hash; + var encryption_cipher: enums.symmetric; + var compression: enums.compression; + var show_version: Boolean; + var show_comment: Boolean; + var integrity_protect: Boolean; + var keyserver: String; + var debug: Boolean; +} + +declare module openpgp.crypto { + interface Mpi { + data: Number, + read(input: String): Number, + write(): String, + } + + /** Generating a session key for the specified symmetric algorithm + @param algo Algorithm to use + */ + function generateSessionKey(algo: enums.symmetric): String; + + /** generate random byte prefix as string for the specified algorithm + @param algo Algorithm to use + */ + function getPrefixRandom(algo: enums.symmetric): String; + + /** Returns the number of integers comprising the private key of an algorithm + @param algo The public key algorithm + */ + function getPrivateMpiCount(algo: enums.symmetric): Number; + + /** Decrypts data using the specified public key multiprecision integers of the private key, the specified secretMPIs of the private key and the specified algorithm. + @param algo Algorithm to be used + @param publicMPIs Algorithm dependent multiprecision integers of the public key part of the private key + @param secretMPIs Algorithm dependent multiprecision integers of the private key used + @param data Data to be encrypted as MPI + */ + function publicKeyDecrypt(algo: enums.publicKey, publicMPIs: Array, secretMPIs: Array, data: Mpi): Mpi; + + /** Encrypts data using the specified public key multiprecision integers and the specified algorithm. + @param algo Algorithm to be used + @param publicMPIs Algorithm dependent multiprecision integers + @param data Data to be encrypted as MPI + */ + function publicKeyEncrypt(algo: enums.publicKey, publicMPIs: Array, data: Mpi): Array; +} + +declare module openpgp.crypto.cfb { + /** This function decrypts a given plaintext using the specified blockcipher to decrypt a message + @param cipherfn the algorithm cipher class to decrypt data in one block_size encryption + @param key binary string representation of key to be used to decrypt the ciphertext. This will be passed to the cipherfn + @param ciphertext to be decrypted provided as a string + @param resync a boolean value specifying if a resync of the IV should be used or not. The encrypteddatapacket uses the "old" style with a resync. Decryption within an encryptedintegrityprotecteddata packet is not resyncing the IV. + */ + function decrypt(cipherfn: String, key: String, ciphertext: String, resync: Boolean): String; + + /** This function encrypts a given with the specified prefixrandom using the specified blockcipher to encrypt a message + @param prefixrandom random bytes of block_size length provided as a string to be used in prefixing the data + @param cipherfn the algorithm cipher class to encrypt data in one block_size encryption + @param plaintext data to be encrypted provided as a string + @param key binary string representation of key to be used to encrypt the plaintext. This will be passed to the cipherfn + @param resync a boolean value specifying if a resync of the IV should be used or not. The encrypteddatapacket uses the "old" style with a resync. Encryption within an encryptedintegrityprotecteddata packet is not resyncing the IV. + */ + function encrypt(prefixrandom: String, cipherfn: String, plaintext: String, key: String, resync: Boolean): String; + + /** Decrypts the prefixed data for the Modification Detection Code (MDC) computation + @param cipherfn cipherfn.encrypt Cipher function to use + @param key binary string representation of key to be used to check the mdc This will be passed to the cipherfn + @param ciphertext The encrypted data + */ + function mdc(cipherfn: Object, key: String, ciphertext: String): String; +} + +declare module openpgp.crypto.hash { + /** Create a hash on the specified data using the specified algorithm + @param algo Hash algorithm type + @param data Data to be hashed + */ + function digest(algo: enums.hash, data: String): String; + + /** Returns the hash size in bytes of the specified hash algorithm type + @param algo Hash algorithm type + */ + function getHashByteLength(algo: enums.hash): Number; +} + +declare module openpgp.crypto.random { + /** Create a secure random big integer of bits length + @param bits Bit length of the MPI to create + */ + function getRandomBigInteger(bits: Number): Number; + + /** Retrieve secure random byte string of the specified length + @param length Length in bytes to generate + */ + function getRandomBytes(length: Number): String; + + /** Helper routine which calls platform specific crypto random generator + @param buf + */ + function getRandomValues(buf: Uint8Array): void; + + /** Return a secure random number in the specified range + @param from Min of the random number + @param to Max of the random number (max 32bit) + */ + function getSecureRandom(from: Number, to: Number): Number; +} + +declare module openpgp.crypto.signature { + /** Create a signature on data using the specified algorithm + @param hash_algo hash Algorithm to use + @param algo Asymmetric cipher algorithm to use + @param publicMPIs Public key multiprecision integers of the private key + @param secretMPIs Private key multiprecision integers which is used to sign the data + @param data Data to be signed + */ + function sign(hash_algo: enums.hash, algo: enums.publicKey, publicMPIs: Array, secretMPIs: Array, data: String): Mpi; + + /** + @param algo public Key algorithm + @param hash_algo Hash algorithm + @param msg_MPIs Signature multiprecision integers + @param publickey_MPIs Public key multiprecision integers + @param data Data on where the signature was computed on + */ + function verify(algo: enums.publicKey, hash_algo: enums.hash, msg_MPIs: Array, publickey_MPIs: Array, data: String): Boolean; +} + +declare module openpgp.enums { + + enum armor { + multipart_section, + multipart_last, + signed, + message, + public_key, + private_key + } + + enum compression { + uncompressed, + zip, + zlib, + bzip2 + } + + enum hash { + md5, + sha1, + ripemd, + sha256, + sha384, + sha512, + sha224 + } + + enum packet { + publicKeyEncryptedSessionKey, + signature, + symEncryptedSessionKey, + onePassSignature, + secretKey, + publicKey, + secretSubkey, + compressed, + symmetricallyEncrypted, + marker, + literal, + trust, + userid, + publicSubkey, + userAttribute, + symEncryptedIntegrityProtected, + modificationDetectionCode, + } + + enum publicKey { + rsa_encrypt_sign, + rsa_encrypt, + rsa_sign, + elgamal, + dsa + } + + enum symmetric { + plaintext, + idea, + tripledes, + cast5, + blowfish, + aes128, + aes192, + aes256, + twofish + } +} + +declare module openpgp.key { + + interface KeyResult { + keys: Array, + err: Array + } + + /** Class that represents an OpenPGP key. Must contain a primary key. Can contain additional subkeys, signatures, user ids, user attributes. + */ + interface Key { + armor(): String, + decrypt(passphrase: String): Boolean, + } + + /** Generates a new OpenPGP key. Currently only supports RSA keys. Primary and subkey will be of same type. + + @param options + */ + function generate(options: KeyOptions): Key; + + /** Reads an OpenPGP armored text and returns one or multiple key objects + + @param armoredText text to be parsed + */ + function readArmored(armoredText: String): KeyResult; + +} + +declare module openpgp.message { + /** Class that represents an OpenPGP message. Can be an encrypted message, signed message, compressed message or literal message + */ + interface Message { + /** Returns ASCII armored text of message + */ + armor(): String, + + /** Decrypt the message + @param privateKey private key with decrypted secret data + */ + decrypt(privateKey: key.Key): Array, + + /** Encrypt the message + @param keys array of keys, used to encrypt the message + */ + encrypt(keys: Array): Array, + + /** Returns the key IDs of the keys to which the session key is encrypted + */ + getEncryptionKeyIds(): Array, + + /** Get literal data that is the body of the message + */ + getLiteralData(): String, + + /** Returns the key IDs of the keys that signed the message + */ + getSigningKeyIds(): Array, + + /** Get literal data as text + */ + getText(): String, + + /** Sign the message (the literal data packet of the message) + @param privateKey private keys with decrypted secret key data for signing + */ + sign(privateKey: Array): Message, + + /** Unwrap compressed message + */ + unwrapCompressed(): Message, + + /** Verify message signatures + @param keys array of keys to verify signatures + */ + verify(keys: Array): Array, + } + + /** creates new message object from binary data + @param bytes + */ + function fromBinary(bytes: String): Message; + + /** creates new message object from text + @param text + */ + function fromText(text: String): Message; + + /** reads an OpenPGP armored message and returns a message object + + @param armoredText text to be parsed + */ + function readArmored(armoredText: String): Message; +} + +declare module openpgp.packet { + + /** Allocate a new packet from structured packet clone + @param packetClone packet clone + */ + function fromStructuredClone(packetClone: Object): Object + + /** Allocate a new packet + @param property name from enums.packet + */ + function newPacketFromTag(tag: String): Object; +} + +declare module openpgp.util { + /** Convert an array of integers(0.255) to a string + @param bin An array of (binary) integers to convert + */ + function bin2str(bin: Array): String; + + /** Calculates a 16bit sum of a string by adding each character codes modulus 65535 + @param text String to create a sum of + */ + function calc_checksum(text: String): Number; + + /** Convert a string of utf8 bytes to a native javascript string + @param utf8 A valid squence of utf8 bytes + */ + function decode_utf8(utf8: String): String + + /** Convert a native javascript string to a string of utf8 bytes + param str The string to convert + */ + function encode_utf8(str: String): String + + /** Return the algorithm type as string + */ + function get_hashAlgorithmString(): String + + /** Get native Web Cryptography api. The default configuration is to use the api when available. But it can also be deactivated with config.useWebCrypto + */ + function getWebCrypto(): Object + + /** Create binary string from a hex encoded string + @param str Hex string to convert + */ + function hex2bin(str: String): String; + + /** Creating a hex string from an binary array of integers (0..255) + @param str Array of bytes to convert + */ + function hexidump(str: String): String; + + /** Create hexstring from a binary + @param str String to convert + */ + function hexstrdump(str: String): String; + + /** Helper function to print a debug message. Debug messages are only printed if + @param str String of the debug message + */ + function print_debug(str: String): void; + + /** Helper function to print a debug message. Debug messages are only printed if + @param str String of the debug message + */ + function print_debug_hexstr_dump(str: String): void; + + /** Shifting a string to n bits right + @param value The string to shift + @param bitcount Amount of bits to shift (MUST be smaller than 9) + */ + function shiftRight(value: String, bitcount: Number): String; + + /** Convert a string to an array of integers(0.255) + @param str String to convert + */ + function str2bin(str: String): Array; + + /** Convert a string to a Uint8Array + @param str String to convert + */ + function str2Uint8Array(str: String): Uint8Array; + + /** Convert a Uint8Array to a string. This currently functions the same as bin2str. + @param bin An array of (binary) integers to convert + */ + function Uint8Array2str(bin: Uint8Array): String; + + +} \ No newline at end of file