My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
jscrypt  
jscrypt module
doc
Updated Nov 9, 2009 by sou...@gmail.com

If something seems wrong or incomplete, please enter a comment at the bottom of this page.



- source - main - QA -

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.


jscrypt static members

- top - revision -

Static functions

Base64Encode

string Base64Encode( string )
Encode the given string using base64 encoding.

Base64Decode

string Base64Decode( string )
Encode the given string using base64 encoding.

HexEncode

string HexEncode( string )
Encode the given string using hexadecimal encoding.

HexDecode

string HexDecode( string )
Decode the given string using hexadecimal encoding.

class jscrypt::Cipher

- top - revision -

constructor

constructor( modeName, cipherName, key, [IV], [arg], [rounds] )
Constructs a Cipher object that use cipherName algorithm for performing encryption and decryption.
arguments:
  1. string modeName: is the block cipher modes of operation:
    • ECB (Electronic codebook)
    • CFB (Cipher feedback)
    • OFB (Output Feedback)
    • CBC (Cipher Block Chaining)
    • CTR (CounTeR)
    • LRW
    • F8
  2. string cipherName: is the name of the cipher algorithm used for data encryption and decryption:
    • 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
  3. string key: is the encryption/decryption key.
  4. string IV:
  5. 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.
    beware:
    This argument is invalid in ECB block mode.
  6. string arg: is either the tweak key for the LRW mode or the salt value for the F8 mode. In other modes arg must be undefined.
  7. beware:
    In LRW mode, the tweak value must have the same length as the key.
  8. integer rounds: is the number of rounds to do with the current sipher. If the argument is omitted, a default value is used.

Methods

Encrypt

bstring Encrypt( data )
Encrypts the given data using the current cipher.

Decrypt

bstring Decrypt( data )
Decrypts the given data using the current cipher.

Properties

blockLength

integer blockLength
Is the block length of the current cipher.

keySize

integer keySize
Is the key size of the current cipher.

name

string name
Is the name of the current cipher.

IV

string IV
Set or get the current initialization vector of the cipher.

Static properties

list

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.

class jscrypt::AsymmetricCipher

- top - revision -

constructor

constructor( cipherName, hashName [, prngObject] [, PKCSVersion = 1_OAEP] )
Creates a new Asymmetric Cipher object.
arguments:
  1. string cipherName: is a string that contains the name of the Asymmetric Cipher algorithm:
    • rsa
    • ecc
    • dsa
  2. string hashName: is the hash that will be used to create the PSS (Probabilistic Signature Scheme) encoding. It should be the same as the hash used to hash the message being signed. See Hash class for available names.
  3. Object 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.
  4. string PKCSVersion: is a string that contains the padding version used by RSA to encrypt/decrypd data:
    • 1_OAEP (for PKCS #1 v2.1 encryption)
    • 1_V1_5 (for PKCS #1 v1.5 encryption)
    If omitted, the default value is 1_OAEP. Only RSA use this argument.

Methods

CreateKeys

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 bits

supported ECC keySize: 112, 128, 160, 192, 224, 256, 384, 521, 528 bits

supported DSA keySize (Bits of Security): ???, 80, 120, 140, 160, ??? bits

Encrypt

bstring 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).

Decrypt

bstring 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.

Sign

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)

VerifySignature

boolean 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

blockLength

integer blockLength
is the maximum length of data that can be processed at once.

keySize

integer keySize
is the size of the current key.

key

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' );

class jscrypt::Hash

- top - revision -

This class is used to create block Hash objects.

constructor

constructor( hashName )
Creates a new hash. hashName is a string that contains the name of the hash:
  • whirlpool
  • sha512
  • sha384
  • sha256
  • sha224
  • sha1
  • md5
  • md4
  • md2
  • tiger
  • rmd128
  • rmd160
  • rmd256
  • rmd320
  • chc_hash
note:
chc_hash is a special hash that allows to create a hash from a cipher (Cipher Hash Construction). See CipherHash() static function.

Methods

Init

Init()
Initialize the hash state.

Process

void Process( data )
Process a block of data though the hash.

Done

bstring 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

call operator

bstring call operator( data )
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

name

string name
Name of the current hash.

blockSize

integer blockSize
Input block size in octets.

length

integer length
Size of the digest in octets.

inputLength

integer inputLength
Length of the processed data.

Static functions

CipherHash

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

list

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() );

class jscrypt::Prng

- top - revision -

This class is used to create pseudo random number generators objects.

constructor

constructor( prngName )
Constructs a pseudo random number generator object using the given algorithm.
arguments:
  1. string prngName: is a string that contains the name of the Asymmetric Cipher algorithm:
    • yarrow
    • fortuna
    • rc4
    • sprng
    • sober128

Methods

call operator

string 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

AddEntropy

void AddEntropy( data )
Add data as entropy (randomness) to the current prng.

AutoEntropy

void AutoEntropy( size )
Automaticaly add size bits of entropy to the current prng.

Static properties

state

bstring state
is the current state of the prng.

name

string name
is the name of the current prng.

list

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.

class jscrypt::CryptError

- top -


- top - main -


Sign in to add a comment
Powered by Google Project Hosting