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:
- source[buffer]: The Titanium.Buffer to decode
- type[string]: The format of the returned string:
- crypto.TYPE_BASE64STRING - result is a base64 encoded string
- crypto.TYPE_HEXSTRING - result is a hexadecimal formatted string
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:
- source[object]: The data to encode (see 'type' for supported object types)
- dest[buffer]: The Titanium.Buffer to receive the encoded data
- destPosition[int]: The position in dest to set the encoded data (optional, default is 0)
- type[string]: The data type of the source object:
- crypto.TYPE_BASE64STRING - source is a base64 encoded string
- crypto.TYPE_HEXSTRING - source is a hexadecimal formatted string
- crypto.TYPE_BLOB - source is a Titanium.Blob object
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
- Copy the module zip file into the root folder of your Titanium application or in the Titanium system folder (e.g. /Library/Application Support/Titanium).
- Set the
<module>
element in tiapp.xml, such as this:
<modules>
<module version="2.0" platform="android">ti.crypto</module>
</modules>
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.