Node - Js v0.10.26 Manual & Documentation: Index View On Single Page View As JSON
Node - Js v0.10.26 Manual & Documentation: Index View On Single Page View As JSON
@nodejs
http://nodejs.org/docs/v0.10.26/api/crypto.html
21/4/2014
Crypto
S t a b i l i t y :2-U n s t a b l e ;A P Ic h a n g e sa r eb e i n gd i s c u s s e df o r f u t u r ev e r s i o n s . B r e a k i n gc h a n g e sw i l lb em i n i m i z e d . S e eb e l o w .
Use r e q u i r e ( ' c r y p t o ' ) to access this module. The crypto module offers a way of encapsulating secure credentials to be used as part of a secure HTTPS net or http connection. It also offers a set of wrappers for OpenSSL's hash, hmac, cipher, decipher, sign and verify methods.
crypto.getCiphers()
Returns an array with the names of the supported ciphers. Example:
v a rc i p h e r s=c r y p t o . g e t C i p h e r s ( ) ; c o n s o l e . l o g ( c i p h e r s ) ;/ /[ ' A E S 1 2 8 C B C ' ,' A E S 1 2 8 C B C H M A C S H A 1 ' ,. . . ]
crypto.getHashes()
Returns an array with the names of the supported hash algorithms. Example:
v a rh a s h e s=c r y p t o . g e t H a s h e s ( ) ; c o n s o l e . l o g ( h a s h e s ) ;/ /[ ' s h a ' ,' s h a 1 ' ,' s h a 1 W i t h R S A E n c r y p t i o n ' ,. . . ]
crypto.createCredentials(details)
Creates a credentials object, with the optional details being a dictionary with keys:
p f x: A string or buffer holding the PFX or PKCS12 encoded private key, certificate and CA certificates k e y: A string holding the PEM encoded private key p a s s p h r a s e: A string of passphrase for the private key or pfx c e r t: A string holding the PEM encoded certificate c a: Either a string or list of strings of PEM encoded CA certificates to trust. c r l: Either a string or list of strings of PEM encoded CRLs (Certificate Revocation List) c i p h e r s : A string describing the ciphers to use or exclude. Consult
http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT for details on the format. If no 'ca' details are given, then node.js will use the default publicly trusted list of CAs as given in http://mxr.mozilla.org/mozilla/source/security/nss/lib/ckfw/builtins/certdata.txt.
crypto.createHash(algorithm)
Creates and returns a hash object, a cryptographic hash with the given algorithm which can be used to generate hash digests.
a l g o r i t h m is dependent on the available algorithms supported by the version of OpenSSL on the platform.
Examples are ' s h a 1 ' , ' m d 5 ' , ' s h a 2 5 6 ' , ' s h a 5 1 2 ' , etc. On recent releases, o p e n s s ll i s t m e s s a g e http://nodejs.org/docs/v0.10.26/api/crypto.html 2/9
21/4/2014
Class: Hash
The class for creating hash digests of data.
It is a stream that is both readable and writable. The written data is used to compute the hash. Once the writable side of the stream is ended, use the r e a d ( ) method to get the computed hash digest. The legacy u p d a t e and
d i g e s t methods are also supported.
Returned by c r y p t o . c r e a t e H a s h .
hash.update(data, [input_encoding])
Updates the hash content with the given d a t a , the encoding of which is given in i n p u t _ e n c o d i n g and can be
' u t f 8 ' , ' a s c i i ' or ' b i n a r y ' . If no encoding is provided and the input is a string an encoding of ' b i n a r y '
is enforced. If d a t a is a B u f f e r then i n p u t _ e n c o d i n g is ignored. This can be called many times with new data as it is streamed.
hash.digest([encoding])
Calculates the digest of all of the passed data to be hashed. The e n c o d i n g can be ' h e x ' , ' b i n a r y ' or
' b a s e 6 4 ' . If no encoding is provided, then a buffer is returned.
Note: h a s h object can not be used after d i g e s t ( ) method has been called.
crypto.createHmac(algorithm, key)
Creates and returns a hmac object, a cryptographic hmac with the given algorithm and key.
It is a stream that is both readable and writable. The written data is used to compute the hmac. Once the writable side of the stream is ended, use the r e a d ( ) method to get the computed digest. The legacy u p d a t e and
d i g e s t methods are also supported. a l g o r i t h m is dependent on the available algorithms supported by OpenSSL - see createHash above. k e y is the
Class: Hmac
http://nodejs.org/docs/v0.10.26/api/crypto.html
#
3/9
21/4/2014
hmac.update(data)
Update the hmac content with the given d a t a . This can be called many times with new data as it is streamed.
hmac.digest([encoding])
Calculates the digest of all of the passed data to the hmac. The e n c o d i n g can be ' h e x ' , ' b i n a r y ' or
' b a s e 6 4 ' . If no encoding is provided, then a buffer is returned.
Note: h m a c object can not be used after d i g e s t ( ) method has been called.
crypto.createCipher(algorithm, password)
Creates and returns a cipher object, with the given algorithm and password.
a l g o r i t h m is dependent on OpenSSL, examples are ' a e s 1 9 2 ' , etc. On recent releases, o p e n s s ll i s t -
c i p h e r a l g o r i t h m s will display the available cipher algorithms. p a s s w o r d is used to derive key and IV, which
must be a ' b i n a r y ' encoded string or a buffer. It is a stream that is both readable and writable. The written data is used to compute the hash. Once the writable side of the stream is ended, use the r e a d ( ) method to get the computed hash digest. The legacy u p d a t e and
d i g e s t methods are also supported.
an initialization vector.
k e y and i v must be ' b i n a r y ' encoded strings or buffers.
Class: Cipher
Class for encrypting data. Returned by c r y p t o . c r e a t e C i p h e r and c r y p t o . c r e a t e C i p h e r i v .
Cipher objects are streams that are both readable and writable. The written plain text data is used to produce the encrypted data on the readable side. The legacy u p d a t e and f i n a l methods are also supported.
The o u t p u t _ e n c o d i n g specifies the output format of the enciphered data, and can be ' b i n a r y ' , ' b a s e 6 4 ' or
' h e x ' . If no encoding is provided, then a buffer is returned.
Returns the enciphered contents, and can be called many times with new data as it is streamed.
http://nodejs.org/docs/v0.10.26/api/crypto.html
4/9
21/4/2014
cipher.final([output_encoding])
Returns any remaining enciphered contents, with o u t p u t _ e n c o d i n g being one of: ' b i n a r y ' , ' b a s e 6 4 ' or
' h e x ' . If no encoding is provided, then a buffer is returned.
Note: c i p h e r object can not be used after f i n a l ( ) method has been called.
cipher.setAutoPadding(auto_padding=true)
Y ou can disable automatic padding of the input data to block size. If a u t o _ p a d d i n g is false, the length of the entire input data must be a multiple of the cipher's block size or f i n a l will fail. Useful for non-standard padding, e.g. using 0 x 0 instead of PKCS padding. Y ou must call this before c i p h e r . f i n a l .
crypto.createDecipher(algorithm, password)
Creates and returns a decipher object, with the given algorithm and key. This is the mirror of the createCipher() above.
Class: Decipher
Class for decrypting data. Returned by c r y p t o . c r e a t e D e c i p h e r and c r y p t o . c r e a t e D e c i p h e r i v .
Decipher objects are streams that are both readable and writable. The written enciphered data is used to produce the plain-text data on the the readable side. The legacy u p d a t e and f i n a l methods are also supported.
decipher.final([output_encoding])
Returns any remaining plaintext which is deciphered, with o u t p u t _ e n c o d i n g being one of: ' b i n a r y ' ,
' a s c i i ' or ' u t f 8 ' . If no encoding is provided, then a buffer is returned.
Note: d e c i p h e r object can not be used after f i n a l ( ) method has been called.
decipher.setAutoPadding(auto_padding=true)
Y ou can disable auto padding if the data has been encrypted without standard block padding to prevent d e c i p h e r . f i n a l from checking and removing it. Can only work if the input data's length is a multiple of the ciphers block size. Y ou must call this before streaming data to d e c i p h e r . u p d a t e .
crypto.createSign(algorithm)
Creates and returns a signing object, with the given algorithm. On recent OpenSSL releases, o p e n s s ll i s t http://nodejs.org/docs/v0.10.26/api/crypto.html
#
5/9
21/4/2014
Class: Sign
Class for generating signatures. Returned by c r y p t o . c r e a t e S i g n . Sign objects are writable streams. The written data is used to generate the signature. Once all of the data has been written, the s i g n method will return the signature. The legacy u p d a t e method is also supported.
sign.update(data)
Updates the sign object with data. This can be called many times with new data as it is streamed.
sign.sign(private_key, [output_format])
PEM encoded private key for signing. Returns the signature in o u t p u t _ f o r m a t which can be ' b i n a r y ' , ' h e x ' or ' b a s e 6 4 ' . If no encoding is provided, then a buffer is returned. Note: s i g n object can not be used after s i g n ( ) method has been called.
Calculates the signature on all the updated data passed through the sign. p r i v a t e _ k e y is a string containing the
crypto.createVerify(algorithm)
Creates and returns a verification object, with the given algorithm. This is the mirror of the signing object above.
Class: Verify
Class for verifying signatures. Returned by c r y p t o . c r e a t e V e r i f y .
Verify objects are writable streams. The written data is used to validate against the supplied signature. Once all of the data has been written, the v e r i f y method will return true if the supplied signature is valid. The legacy
u p d a t e method is also supported.
verifier.update(data)
Updates the verifier object with data. This can be called many times with new data as it is streamed.
crypto.createDiffieHellman(prime_length)
http://nodejs.org/docs/v0.10.26/api/crypto.html
#
6/9
21/4/2014
Creates a Diffie-Hellman key exchange object and generates a prime of the given bit length. The generator used is 2.
crypto.createDiffieHellman(prime, [encoding])
Creates a Diffie-Hellman key exchange object using the supplied prime. The generator used is 2. Encoding can be ' b i n a r y ' , ' h e x ' , or ' b a s e 6 4 ' . If no encoding is specified, then a buffer is expected.
Class: DiffieHellman
The class for creating Diffie-Hellman key exchanges. Returned by c r y p t o . c r e a t e D i f f i e H e l l m a n .
diffieHellman.generateKeys([encoding])
Generates private and public Diffie-Hellman key values, and returns the public key in the specified encoding. This key should be transferred to the other party. Encoding can be ' b i n a r y ' , ' h e x ' , or ' b a s e 6 4 ' . If no encoding is provided, then a buffer is returned.
diffieHellman.getPrime([encoding])
Returns the Diffie-Hellman prime in the specified encoding, which can be ' b i n a r y ' , ' h e x ' , or ' b a s e 6 4 ' . If no encoding is provided, then a buffer is returned.
diffieHellman.getGenerator([encoding])
If no encoding is provided, then a buffer is returned.
Returns the Diffie-Hellman generator in the specified encoding, which can be ' b i n a r y ' , ' h e x ' , or ' b a s e 6 4 ' .
diffieHellman.getPublicKey([encoding])
If no encoding is provided, then a buffer is returned.
Returns the Diffie-Hellman public key in the specified encoding, which can be ' b i n a r y ' , ' h e x ' , or ' b a s e 6 4 ' .
diffieHellman.getPrivateKey([encoding])
Returns the Diffie-Hellman private key in the specified encoding, which can be ' b i n a r y ' , ' h e x ' , or
' b a s e 6 4 ' . If no encoding is provided, then a buffer is returned.
diffieHellman.setPublicKey(public_key, [encoding])
Sets the Diffie-Hellman public key. Key encoding can be ' b i n a r y ' , ' h e x ' or ' b a s e 6 4 ' . If no encoding is
http://nodejs.org/docs/v0.10.26/api/crypto.html
7/9
21/4/2014
diffieHellman.setPrivateKey(private_key, [encoding])
Sets the Diffie-Hellman private key. Key encoding can be ' b i n a r y ' , ' h e x ' or ' b a s e 6 4 ' . If no encoding is provided, then a buffer is expected.
crypto.getDiffieHellman(group_name)
Creates a predefined Diffie-Hellman key exchange object. The supported groups are: ' m o d p 1 ' , ' m o d p 2 ' ,
' m o d p 5 ' (defined in RFC 2412) and ' m o d p 1 4 ' , ' m o d p 1 5 ' , ' m o d p 1 6 ' , ' m o d p 1 7 ' , ' m o d p 1 8 ' (defined in RFC
3526). The returned object mimics the interface of objects created by crypto.createDiffieHellman() above, but will not allow to change the keys (with diffieHellman.setPublicKey() for example). The advantage of using this routine is that the parties don't have to generate nor exchange group modulus beforehand, saving both processor and communication time. Example (obtaining a shared secret):
v a rc r y p t o=r e q u i r e ( ' c r y p t o ' ) ; v a ra l i c e=c r y p t o . g e t D i f f i e H e l l m a n ( ' m o d p 5 ' ) ; v a rb o b=c r y p t o . g e t D i f f i e H e l l m a n ( ' m o d p 5 ' ) ; a l i c e . g e n e r a t e K e y s ( ) ; b o b . g e n e r a t e K e y s ( ) ; v a ra l i c e _ s e c r e t=a l i c e . c o m p u t e S e c r e t ( b o b . g e t P u b l i c K e y ( ) ,n u l l ,' h e x ' ) ; v a rb o b _ s e c r e t=b o b . c o m p u t e S e c r e t ( a l i c e . g e t P u b l i c K e y ( ) ,n u l l ,' h e x ' ) ; / *a l i c e _ s e c r e ta n db o b _ s e c r e ts h o u l db et h es a m e* / c o n s o l e . l o g ( a l i c e _ s e c r e t= =b o b _ s e c r e t ) ;
crypto.randomBytes(size, [callback])
Generates cryptographically strong pseudo-random data. Usage:
/ /a s y n c c r y p t o . r a n d o m B y t e s ( 2 5 6 ,f u n c t i o n ( e x ,b u f ){ i f( e x )t h r o we x ; c o n s o l e . l o g ( ' H a v e% db y t e so fr a n d o md a t a :% s ' ,b u f . l e n g t h ,b u f ) ; } ) ; / /s y n c t r y{ v a rb u f=c r y p t o . r a n d o m B y t e s ( 2 5 6 ) ; c o n s o l e . l o g ( ' H a v e% db y t e so fr a n d o md a t a :% s ' ,b u f . l e n g t h ,b u f ) ; }c a t c h( e x ){ / /h a n d l ee r r o r / /m o s tl i k e l y ,e n t r o p ys o u r c e sa r ed r a i n e d }
http://nodejs.org/docs/v0.10.26/api/crypto.html
8/9
21/4/2014
NOTE: Will throw error or invoke callback with error, if there is not enough accumulated entropy to generate cryptographically strong data. In other words, c r y p t o . r a n d o m B y t e s without callback will not block even if all entropy sources are drained.
crypto.pseudoRandomBytes(size, [callback])
Generates non-cryptographically strong pseudo-random data. The data returned will be unique if it is sufficiently long, but is not necessarily unpredictable. For this reason, the output of this function should never be used where unpredictability is important, such as in the generation of encryption keys. Usage is otherwise identical to c r y p t o . r a n d o m B y t e s .
crypto.DEFAULT_ENCODING
The default encoding to use for functions that can take either strings or buffers. The default value is ' b u f f e r ' , which makes it default to using Buffer objects. This is here to make the crypto module more easily compatible with legacy programs that expected ' b i n a r y ' to be the default encoding. Note that new programs will probably expect buffers, so only use this as a temporary measure.
As such, the streaming classes don't have the typical methods found on other Node classes, and many methods accepted and returned Binary-encoded strings by default rather than Buffers. This was changed to use Buffers by default instead. This is a breaking change for some use cases, but not all. For example, if you currently use the default arguments to the Sign class, and then pass the results to the Verify class, without ever inspecting the data, then it will continue to work as before. Where you once got a binary string and then presented the binary string to the Verify object, you'll now get a Buffer, and present the Buffer to the Verify object. However, if you were doing things with the string data that will not work properly on Buffers (such as, concatenating them, storing in databases, etc.), or you are passing binary strings to the crypto functions without an encoding argument, then you will need to start providing encoding arguments to specify which encoding you'd like to use. To switch to the previous style of using binary strings by default, set the c r y p t o . D E F A U L T _ E N C O D I N G field to 'binary'. Note that new programs will probably expect buffers, so only use this as a temporary measure.
Node.js
Download
About
npm Registry
Docs
Blog
Community
Logos
Jobs
@nodejs
http://nodejs.org/docs/v0.10.26/api/crypto.html
9/9