crypto Module

Description

This module provides access to various encryption and decryption algorithms, and string encodings.

Getting Started

View the Using Titanium Modules document for instructions on getting started with using this module in your application.

Accessing the crypto Module

To access this module from JavaScript, you would do the following:

var crypto = require("ti.crypto");

The crypto variable is a reference to the Module object.

Methods

createCryptor

Creates a crypto.cryptor object for encrypting and decrypting data. See Cryptor for parameters of this method as well as the methods for performing the actual encryption and decryption operations.

decodeData

Decodes a source buffer into a string using the specified data type. This is a codec method used for converting data from a source buffer into a displayable string.

Arguments

Takes one argument, a dictionary with keys:

Return Value

Returns the decoded string

Example

<pre> // Set the value of the encrypted text (base64 encoded for readability) cipherTextField.value = crypto.decodeData({ source: buffer, type: App.crypto.TYPE_BASE64STRING }); </pre>

encodeData

Encodes the source into the destination buffer using the specified data type. This is a codec method used for converting data to a destination buffer.

Arguments

Takes one argument, a dictionary with keys:

Return Value

Returns the position after the encoded data inside the destination.

Example

<pre> // Load the buffer with the base64encoded value from the encrypted text field var buffer = Ti.createBuffer({ length: cipherTextField.value.length }); var length = crypto.encodeData({ source: cipherTextField.value, dest: buffer, type: App.crypto.TYPE_BASE64STRING }); if (length < 0) { Ti.API.info('ERROR: Buffer too small'); return; } </pre>

Constants

Cryptor Status Codes

crypto.STATUS_SUCCESS

crypto.STATUS_ERROR

crypto.STATUS_PARAMERROR

crypto.STATUS_BUFFERTOOSMALL

crypto.STATUS_MEMORYFAILURE

crypto.STATUS_ALIGNMENTERROR

crypto.STATUS_DECODEERROR

crypto.STATUS_UNIMPLEMENTED

Cryptor Operations

crypto.ENCRYPT

crypto.DECRYPT

Cryptor Algorithms

crypto.ALGORITHM_AES128

crypto.ALGORITHM_DES

crypto.ALGORITHM_3DES

crypto.ALGORITHM_CAST

crypto.ALGORITHM_RC4 [iOS Only]

crypto.ALGORITHM_RC2

Cryptor Options

crypto.OPTION_PKCS7PADDING

crypto.OPTION_ECBMODE

Cryptor Key Sizes

crypto.KEYSIZE_AES128

crypto.KEYSIZE_AES192

crypto.KEYSIZE_AES256

crypto.KEYSIZE_DES

crypto.KEYSIZE_3DES

crypto.KEYSIZE_MINCAST

crypto.KEYSIZE_MAXCAST

crypto.KEYSIZE_MINRC4 [iOS Only]

crypto.KEYSIZE_MAXRC4 [iOS Only]

crypto.KEYSIZE_MINRC2

crypto.KEYSIZE_MINRC2

Cryptor Block Sizes

crypto.BLOCKSIZE_AES128

crypto.BLOCKSIZE_DES

crypto.BLOCKSIZE_3DES

crypto.BLOCKSIZE_CAST

crypto.BLOCKSIZE_RC2

Data Types

crypto.TYPE_BLOB - A Titanium.Blob object

crypto.TYPE_HEXSTRING - A hexadecimal formatted string. Each byte is represented by 2 hexadecimal numbers.

crypto.TYPE_BASE64STRING - A Base64 encoded string

Using this module

Usage

See example.

Author

Dawson Toth

Module History

View the change log for this module.

Feedback and Support

Please direct all questions, feedback, and concerns to info@appcelerator.com.

License

Copyright(c) 2010-2013 by Appcelerator, Inc. All Rights Reserved. Please see the LICENSE file included in the distribution for further details.