|
|
jscrypt
jscrypt module
jscrypt module
jscrypt is a cryptographic toolkit that provides developers with a vast array of well known published block ciphers, one-way hash functions, chaining modes, pseudo-random number generators, public key cryptography and a plethora of other routines. The underlying native code of this module is based on LibTom library.
Static functions
- string Base64Encode( string )
Encode the given string using base64 encoding.
- string Base64Decode( string )
Encode the given string using base64 encoding.
- string HexEncode( string )
Encode the given string using hexadecimal encoding.
- string HexDecode( string )
Decode the given string using hexadecimal encoding.
jscrypt::AsymmetricCipher class
Functions
- Constructor( cipherName, hashName [, prngObject] [, PKCSVersion = 1_OAEP] )
- rsa
- ecc
- dsa
- 1_OAEP (for PKCS #1 v2.1 encryption)
- 1_V1_5 (for PKCS #1 v1.5 encryption)
Creates a new Asymmetric Cipher object. cipherName is a string that contains the name of the Asymmetric Cipher algorithm:
hashName indicates which hash will be used to create the PSS encoding. It should be the same as the hash used to hash the message being signed. See Hash class for available names.
prngObject is an instantiated Prng object. Its current state will be used for key creation, data encryption/decryption, data signature/signature check.
This argument can be ommited if you aim to decrypt data only.PKCSVersion is a string that contains the padding version used by RSA to encrypt/decrypd data.
If omitted, the default value is 1_OAEPOnly RSA use this argument.
- CreateKeys( keySize )
Create RSA public and private keys.keySize is the size of the key in bits (the value of keySize is the modulus size).
note:
supported RSA keySize: from 1024 to 4096 bitssupported ECC keySize: 112, 128, 160, 192, 224, 256, 384, 521, 528 bits
supported DSA keySize (Bits of Security): ???, 80, 120, 140, 160, ??? bits
- string Encrypt( data [, lparam] )
This function returns the encrypted data using a previously created or imported public key.data is the string to encrypt (usualy cipher keys).
- string Decrypt( encryptedData [, lparam] )
This function decrypts the given encryptedData using a previously created or imported private key.encryptedData is the string that has to be decrypted (usualy cipher keys).
note:
The lparam variable is an additional system specific tag that can be applied to the encoding. This is useful to identify which system encoded the message. If no variance is desired then lparam can be ignored or set to <undefined>.If it does not match what was used during encoding this function will not decode the packet.
When performing v1.5 RSA decryption, the hash and lparam parameters are totally ignored.
- string Sign( data [, saltLength] )
This function returns the signature of the given data. Because this process is slow, this function usualy used to sign a small amount of data, like hash digest.saltLength is only used with RSA signatures. (default value is 16)
- string VerifySignature( data, signature [, saltLength] )
This function returns <true> if the data match the data used to create the signature.saltLength is only used with RSA signatures. (default value is 16)
Properties
- string privateKey
The private key encoded using PKCS #1. (Public Key Cryptographic Standard #1 v2.0 padding)
- string publicKey
The public key encoded using PKCS #1. (Public Key Cryptographic Standard #1 v2.0 padding)
Example
Data (or key) encryption using RSA:
LoadModule('jsstd');
LoadModule('jscrypt');
var fortuna = new Prng('fortuna');
fortuna.AutoEntropy(123); // give more entropy
//Alice
var alice = new AsymmetricCipher('RSA', 'md5', fortuna);
alice.CreateKeys(1024);
var publicKey = alice.publicKey;
//Bob
var bob = new AsymmetricCipher('RSA', 'md5', fortuna);
bob.publicKey = publicKey;
var encryptedData = bob.Encrypt('Alice, I love you !');
//Alice
Print( alice.Decrypt(encryptedData), '\n' );
jscrypt::Hash class
This class is used to create block Hash objects.
Functions
- Constructor( hashName )
- whirlpool
- sha512
- sha384
- sha256
- sha224
- sha1
- md5
- md4
- md2
- tiger
- rmd128
- rmd160
- rmd256
- rmd320
- chc_hash
Creates a new hash hashName is a string that contains the name of the hash:
note:
chc_hash is a special hash that allows to create a hash from a cipher (Cipher Hash Construction). See CipherHash() static function.
- Init()
Initialize the hash state.
- Process( string )
Process a block of memory though the hash.
- string Done()
Terminate the hash and get the digest in a binary format.example:
LoadModule('jsstd'); LoadModule('jscrypt'); var md5 = new Hash('md5'); md5.Process('foo'); md5.Process('bar'); Print( HexEncode( md5.Done(), '\n' ) ); // prints: 3858F62230AC3C915F300C664312C63F
- string Call operator( string )
This is the call operator of the object. It simplifies the usage of hashes and allows a digest calculation in one call only. When called with a string as argument, it Process a block of memory though the hash Compute the hash until the function is called without arguments or end is <true>. In this case, the function returns the hash of the whole given data.beware:
Using this methode to compute a digest automaticaly resets previous state let by Init(), Process() or Done().example:
LoadModule('jsstd'); LoadModule('jscrypt'); var md5 = new Hash('md5'); Print( HexEncode( md5('foobar') ) ); // prints: 3858F62230AC3C915F300C664312C63F
Properties
- string name
Name of the current hash.
- number blockSize
Input block size in octets.
- number length
Size of the digest in octets.
- number inputLength
Length of the processed data.
Static functions
- CipherHash( cipherName )
Initialize the CHC (chc_hash) state with cipherName cipher.An addition to the suite of hash functions is the Cipher Hash Construction or CHC mode. In this mode applicable block ciphers (such as AES) can be turned into hash functions that other functions can use. In particular this allows a cryptosystem to be designed using very few moving parts.
Static properties
- object list
Contains the list of all available hash and their feature. The list is a javascript object that map hash names (key) with another object (value) that contain information.example:
LoadModule('jsstd'); LoadModule('jscrypt'); Print('hash list: ' + Hash.list.toSource() );
jscrypt::Prng class
This class is used to create pseudo random number generators objects.
Functions
- Constructor( prngName )
- yarrow
- fortuna
- rc4
- sprng
- sober128
Constructs a pseudo random number generator object using the given algorithm:
- AddEntropy( data )
Add data as entropy (randomness) to the current prng.
- AutoEntropy( size )
Automaticaly add size bits of entropy to the current prng.
- Call operator( size )
Returns size bytes of pseudo-random data.example:
LoadModule('jsstd'); LoadModule('jscrypt'); var myGen = new Prng('yarrow'); myGen.AutoEntropy(128); // give more entropy Print(HexEncode(myGen(100))); // prints random data
Static properties
- object list
Contains the list of all available prng and their feature. The list is a javascript object that map cipher names (key) with another object (value) that contain information.
jscrypt::Cipher class
Functions
- Constructor( modeName, cipherName, key, IV, arg, rounds )
- ECB (Electronic codebook)
- CFB (Cipher feedback)
- OFB (Output Feedback)
- CBC (Cipher Block Chaining)
- CTR (CounTeR)
- LRW
- F8
- blowfish
- rc5
- rc6
- rc2
- saferp
- safer_k64, safer_k128, safer_sk64, safer_sk128
- rijndael, aes
- rijndael_enc, aes_enc
- xtea
- twofish
- des &des3
- cast5
- noekeon
- skipjack
- khazad
- anubis
- kseed
- kasumi
Constructs a Cipher object using the given information:modeName is the block cipher modes of operation:
cipherName is the name of the cipher used for data encryption/decryption:
key is the encryption/decryption key:
The encryption key.IV is the first initialization vector:
The IV value is the initialization vector to be used with the cipher. You must fill the IV yourself and it is assumed they are the same length as the block size of the cipher you choose. It is important that the IV be random for each unique message you want to encrypt.note:
This argument is invalid in ECB mode.arg is either the tweak key for the LRW mode or the salt value for the F8 mode.
note:
In LRW mode, the tweak value must have the same size as the keynote:
In other modes arg must be undefined.rounds is the number of rounds to use:
The requested number of rounds.note:
If the argument is omitted, a default value is used.
- string Encrypt( string )
Encrypts the given string using the current cipher.
- string Decrypt( string )
Decrypts the given string using the current cipher.
Properties
- string IV
Set or get the current initialization vector of the cipher.
- number blockLength
Is the block length of the current cipher.
- number keySize
Is the key size of the current cipher.
- string name
Is the name of the current cipher.
Static properties
- object list
Contains the list of all available ciphers and their feature. The list is a javascript object that map cipher names (key) with another object (value) that contain information.
Sign in to add a comment
