SHA

Description

A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined in FIPS 180-1. Also includes a Base64 encoder.

Original implementation:

Copyright Paul Johnston 2000 - 2009. Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet Distributed under the BSD License See http://pajhome.org.uk/crypt/md5 for details.

Inherits from:

Constructor Method

The constructor is invoked, when a new instance of SHA is created.

To create a new instance use SHA.nu( chrsz ) or new SHA( chrsz ).

constructor( chrsz )

Description

Constructs an instance of SHA

Parameters

chrsz The input character size, in bits. Optional.


Class Methods

implement( interface )

Inherited from: HClass

Description

Copies the members of interface to a HClass or a class inherited from HClass.

Usage:

Defines an interface:
// Create an interface class that calculates the area
var AreaInterface = HClass.extend({
  constructor: null,
  area: function() {
    return this.getWidth() * this.getHeight();
  }
});

// Create a class that contains the coordinates of a rectangle. var Rectangle = HClass.extend({ constructor: function( x, y, width, height ) { this.x = x; this.y = y; this.width = width; this.height = height; }, getWidth: function() { return this.width; }, getHeight: function() { return this.height; } });

// Makes the Rectangle class implement the AreaInterface Rectangle.implement( AreaInterface );

nu( )

Inherited from: HClass

Class members:

Description

nu is a protected class method.

It does the same thing as the new keyword, but it is a class method.

Parameters:

Any parameters are passed on to the constructor method.

Usage:

Creates a class MyClass, then creates two instances of it: myClassInstance1 and myClassInstance2. The end result is the same.
var MyClass = HClass.extend({
  name: 'Foo',
  constructor: function( param1, param2 ){
    var logMsg = [ 'param1:', param1,
                   'param2:', param2,
                   'name:', this.name ];
    console.log( logMsg.join(' ') );
  }
});
// Construction using the new keyword:
var myClassInstance1 = new MyClass( 'one', 'two' );
// Construction using the nu method:
var myClassInstance2 = MyClass.nu( 'one', 'two' );
However, when extending in place, the benefits of the nu method become obvious.

// Extending in place with the new keyword:
var myExtendedClassInstance1 = new (
  MyClass.extend({
    name: 'Bar'
  })
)( 'one', 'two' );
// The same as above, but using the nu method:
var myExtendedClassInstance2 = MyClass.extend({
  name: 'Bar'
}).nu( 'one', 'two' );

extend( instance, static )

Inherited from: HClass

Description

The class method used to extend a class.

If the member name constructor is defined as null instead of a function block in the instance object block, the class is defined as a singleton (single instance class).

Parameters:

instance The object that defines the class instance members.
static The object that defines the static class members.

Returns:

Returns the extended class object.

Usage:

var Point = HClass.extend({
  constructor: function( x, y ) {
    this.x = x;
    this.y = y;
  }
});
var Rectangle = Point.extend({
  // Instance members:
  constructor: function( x, y, width, height ) {
    this.base( x, y );
    this.width = width;
    this.height = height;
  },
  getWidth: function() {
    return this.width;
  },
  getHeight: function() {
    return this.height;
  }
},

// Class Members { description: "this is a Rectangle", getClass: function() { return this; } });


Instance Methods

chrsz( )

Returns the number of bits per input character. The default is 8.


base64Pad( )

Returns the Base64 padding character. Is '=' by default.


strSHA1( s )

Description

Calculates the SHA1 of a string and returns the result as a raw string.

Parameters

s The input string.

Returns

A raw string containing the SHA1 result.


hexCase( )

Description

Returns the letter case of the hexadecimal output.

Returns

1 if upporcase, 0 if lowercase.


hexSHA1( s )

Description

Calculates the SHA1 of a string and returns the result encoded in hexadecimal.

Parameters

s The input string.

Returns

A hexadecimal-encoded string containing the SHA1 result.


setBase64Pad( pad )

Description

Sets the Base64 padding character.

Set to '=' (default) for strict RFC compliance.

Parameters

pad The padding character


b64SHA1( s )

Description

Calculates the SHA1 of a string and returns the result encoded in Base64.

Parameters

s The input string.

Returns

A Base64-encoded string containing the SHA1 result.


b64HmacSHA1( key, data )

Description

Calculates the HMAC-SHA1 of a string and returns the result encoded in Base64.

Parameters

key The key to use.
data The input data.

Returns

A Base64-encoded string containing the HMAC-SHA1 result.


str2Base64( str )

Description

Encodes a string to Base64.

Parameters

str The input data.

Returns

The Base64 encoded version of the input data.


setHexCase( case )

Descrition

Sets the letter case of the hexadecimal output.

Parameters:

case 1: Upper case 0: Lower case


setChrsz( bits )

Description

Sets the number of bits per input character.

Parameters

bits Amount of bits per input character. 8 for ascii, 16 for unicode.


hexHmacSHA1( key, data )

Description

Calculates the HMAC-SHA1 of a string and returns the result encoded in hexadecimal.

Parameters

key The key to use.
data The input data.

Returns

A hexadecimal-encoded string containing the HMAC-SHA1 result.


strHmacSHA1( key, data )

Description

Calculates the HMAC-SHA1 of a string and returns the result as a raw string.

Parameters

key The key to use.
data The input data.

Returns

A raw string containing the HMAC-SHA1 result.


test( )

Performs a simple self-test to see if the VM is working


toString( )

Inherited from: HClass

Protected instance method, returns the instance represented as a String.


valueOf( )

Inherited from: HClass

Protected instance method, returns the value of an instance.


base( )

Inherited from: HClass

This method can be called from any other method to invoke that method's parent



Other Instance Members

prototype = { }

Inherited from: Object

Description

The prototype member of Object defines the instance members. These are copied to the instance object when the new keyword is called.