Skip to content

Commit

Permalink
Added the Crypto.cfc vendor library.
Browse files Browse the repository at this point in the history
  • Loading branch information
bennadel committed Aug 20, 2012
1 parent 36b8272 commit e2ea735
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -3,3 +3,9 @@

A ColdFusion wrapper for the PuserhApp REST API.

## Vendor Libraries

* (Crypto.cfc by Ben Nadel)[1]


[1]: https://github.com/bennadel/Crypto.cfc
173 changes: 173 additions & 0 deletions vendor/crypto/Crypto.cfc
@@ -0,0 +1,173 @@
<cfscript>

component
output = "false"
hint = "I provide easy access to Java's HMAC security / crypto methods."
{


// I return an initialized component.
function init(){

// Store the MAC class definition so that its static methods can be accessed quickly.
variables.macClass = createObject( "java", "javax.crypto.Mac" );

// Return this object reference.
return( this );

}


// ---
// PUBLIC METHODS
// ---


// I hash the given input using the MD5 encoding algorithm and the given secret key. By
// default, the hash is returned as a HEX-encoded string.
function hmacMd5(
String key,
String input,
String encoding = "hex"
){

// Hash the input using Hmac MD5.
var authenticationCode = this._hashInputWithAlgorithmAndKey( "HmacMD5", key, input );

// Return the authentication code in the appropriate encoding.
return(
this._encodeByteArray( authenticationCode, encoding )
);

}


// I hash the given input using the Sha-1 encoding algorithm and the given secret key. By
// default, the hash is returned as a HEX-encoded string.
function hmacSha1(
String key,
String input,
String encoding = "hex"
){

// Hash the input using Hmac Sha-1.
var authenticationCode = this._hashInputWithAlgorithmAndKey( "HmacSHA1", key, input );

// Return the authentication code in the appropriate encoding.
return(
this._encodeByteArray( authenticationCode, encoding )
);

}


// I hash the given input using the Sha-256 encoding algorithm and the given secret key. By
// default, the hash is returned as a HEX-encoded string.
function hmacSha256(
String key,
String input,
String encoding = "hex"
){

// Hash the input using Hmac Sha-256.
var authenticationCode = this._hashInputWithAlgorithmAndKey( "HmacSHA256", key, input );

// Return the authentication code in the appropriate encoding.
return(
this._encodeByteArray( authenticationCode, encoding )
);

}


// ---
// PRIVATE METHODS
// ---


// I encode the byte array / binary value using the given encoding. The Hex-encoding is used
// by default.
function _encodeByteArray(
Any bytes,
String encoding = "hex"
){

// Normalize the encoding value.
encoding = lcase( encoding );

// Checking encoding algorithm.
if (encoding == "hex"){

return(
lcase( binaryEncode( bytes, "hex" ) )
);

} else if (encoding == "base64"){

return(
binaryEncode( bytes, "base64" )
);

} else if (encoding == "binary"){

// No further encoding required.
return( bytes );

}

// If we made it this far, the encoding was not recognized or is not yet supported.
throw(
type = "InvalidEncoding",
message = "The requested encoding method [#encoding#] is not yet supported."
);

}


// I return a MAC generator for the given key and algorithm.
function _getMacInstance( String algorithm, String key ){

// Create the specification for our secret key.
var secretkeySpec = createObject( "java", "javax.crypto.spec.SecretKeySpec" ).init(
toBinary( toBase64( key ) ),
javaCast( "string", algorithm )
);

// Get an instance of our MAC generator for the given hashing algorithm.
var mac = variables.macClass.getInstance(
javaCast( "string", algorithm )
);

// Initialize the Mac with our secret key spec.
mac.init( secretkeySpec );

// Return the initialized Mac generator.
return( mac );

}


// I provide a generic method for creating an Hmac with various algorithms. The hash value
// is returned as a binary value / byte array.
function _hashInputWithAlgorithmAndKey(
String algorithm,
String key,
String input
){

// Create our MAC generator.
var mac = this._getMacInstance( algorithm, key );

// Hash the input.
var hashedBytes = mac.doFinal(
toBinary( toBase64( input ) )
);

return( hashedBytes );

}


}

</cfscript>

0 comments on commit e2ea735

Please sign in to comment.