Merge branch 'v0.4'
authorRyan Dahl <ry@tinyclouds.org>
Wed, 15 Jun 2011 12:43:37 +0000 (14:43 +0200)
committerRyan Dahl <ry@tinyclouds.org>
Wed, 15 Jun 2011 12:43:37 +0000 (14:43 +0200)
Conflicts:
doc/api/modules.markdown
test/simple/test-crypto.js

1  2 
doc/api/crypto.markdown
doc/api/modules.markdown
src/node_crypto.cc
test/simple/test-crypto.js

@@@ -139,65 -139,12 +139,67 @@@ This is the mirror of the signing objec
  Updates the verifier object with data.
  This can be called many times with new data as it is streamed.
  
- ### verifier.verify(cert, signature, signature_format='binary')
+ ### verifier.verify(object, signature, signature_format='binary')
  
- Verifies the signed data by using the `cert` which is a string containing
- the PEM encoded certificate, and `signature`, which is the previously calculated
- signature for the data, in the `signature_format` which can be `'binary'`, `'hex'` or `'base64'`.
+ Verifies the signed data by using the `object` and `signature`. `object` is  a
+ string containing a PEM encoded object, which can be one of RSA public key,
+ DSA public key, or X.509 certificate. `signature` is the previously calculated
+ signature for the data, in the `signature_format` which can be `'binary'`,
+ `'hex'` or `'base64'`.
  
  Returns true or false depending on the validity of the signature for the data and public key.
 +
 +### crypto.createDiffieHellman(prime_length)
 +
 +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='binary')
 +
 +Creates a Diffie-Hellman key exchange object using the supplied prime. The
 +generator used is `2`. Encoding can be `'binary'`, `'hex'`, or `'base64'`.
 +
 +### diffieHellman.generateKeys(encoding='binary')
 +
 +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 `'binary'`, `'hex'`, or `'base64'`.
 +
 +### diffieHellman.computeSecret(other_public_key, input_encoding='binary', output_encoding=input_encoding)
 +
 +Computes the shared secret using `other_public_key` as the other party's
 +public key and returns the computed shared secret. Supplied key is
 +interpreted using specified `input_encoding`, and secret is encoded using
 +specified `output_encoding`. Encodings can be `'binary'`, `'hex'`, or
 +`'base64'`. If no output encoding is given, the input encoding is used as
 +output encoding.
 +
 +### diffieHellman.getPrime(encoding='binary')
 +
 +Returns the Diffie-Hellman prime in the specified encoding, which can be
 +`'binary'`, `'hex'`, or `'base64'`.
 +
 +### diffieHellman.getGenerator(encoding='binary')
 +
 +Returns the Diffie-Hellman prime in the specified encoding, which can be
 +`'binary'`, `'hex'`, or `'base64'`.
 +
 +### diffieHellman.getPublicKey(encoding='binary')
 +
 +Returns the Diffie-Hellman public key in the specified encoding, which can
 +be `'binary'`, `'hex'`, or `'base64'`.
 +
 +### diffieHellman.getPrivateKey(encoding='binary')
 +
 +Returns the Diffie-Hellman private key in the specified encoding, which can
 +be `'binary'`, `'hex'`, or `'base64'`.
 +
 +### diffieHellman.setPublicKey(public_key, encoding='binary')
 +
 +Sets the Diffie-Hellman public key. Key encoding can be `'binary'`, `'hex'`,
 +or `'base64'`.
 +
 +### diffieHellman.setPrivateKey(public_key, encoding='binary')
 +
 +Sets the Diffie-Hellman private key. Key encoding can be `'binary'`, `'hex'`, or `'base64'`.
 +
@@@ -330,38 -330,21 +330,56 @@@ Because `module` provides a `filename` 
  `__filename`), the entry point of the current application can be obtained
  by checking `require.main.filename`.
  
++
 +## AMD Compatibility
 +
 +Node's modules have access to a function named `define`, which may be
 +used to specify the module's return value.  This is not necessary in node
 +programs, but is present in the node API in order to provide
 +compatibility with module loaders that use the Asynchronous Module
 +Definition pattern.
 +
 +The example module above could be structured like so:
 +
 +    define(function (require, exports, module) {
 +      var PI = Math.PI;
 +
 +      exports.area = function (r) {
 +        return PI * r * r;
 +      };
 +
 +      exports.circumference = function (r) {
 +        return 2 * PI * r;
 +      };
 +    });
 +
 +* Only the last argument to `define()` matters.  Other module loaders
 +  sometimes use a `define(id, [deps], cb)` pattern, but since this is
 +  not relevant in node programs, the other arguments are ignored.
 +* If the `define` callback returns a value other than `undefined`, then
 +  that value is assigned to `module.exports`.
 +* **Important**: Despite being called "AMD", the node module loader **is
 +  in fact synchronous**, and using `define()` does not change this fact.
 +  Node executes the callback immediately, so please plan your programs
 +  accordingly.
 +
++
+ ### Accessing the main module
+ When a file is run directly from Node, `require.main` is set to its
+ `module`. That means that you can determine whether a file has been run
+ directly by testing
+     require.main === module
+ For a file `foo.js`, this will be `true` if run via `node foo.js`, but
+ `false` if run by `require('./foo')`.
+ Because `module` provides a `filename` property (normally equivalent to
+ `__filename`), the entry point of the current application can be obtained
+ by checking `require.main.filename`.
++
  ## Addenda: Package Manager Tips
  
  The semantics of Node's `require()` function were designed to be general
Simple merge
@@@ -145,30 -147,16 +147,45 @@@ assert.throws(function() 
    crypto.createHash('sha1').update({foo: 'bar'});
  }, /string or buffer/);
  
 +// Test Diffie-Hellman with two parties sharing a secret,
 +// using various encodings as we go along
 +var dh1 = crypto.createDiffieHellman(256);
 +var p1 = dh1.getPrime('base64');
 +var dh2 = crypto.createDiffieHellman(p1, 'base64');
 +var key1 = dh1.generateKeys();
 +var key2 = dh2.generateKeys('hex');
 +var secret1 = dh1.computeSecret(key2, 'hex', 'base64');
 +var secret2 = dh2.computeSecret(key1, 'binary', 'base64');
 +
 +assert.equal(secret1, secret2);
 +
 +// Create "another dh1" using generated keys from dh1,
 +// and compute secret again
 +var dh3 = crypto.createDiffieHellman(p1, 'base64');
 +var privkey1 = dh1.getPrivateKey();
 +dh3.setPublicKey(key1);
 +dh3.setPrivateKey(privkey1);
 +
 +assert.equal(dh1.getPrime(), dh3.getPrime());
 +assert.equal(dh1.getGenerator(), dh3.getGenerator());
 +assert.equal(dh1.getPublicKey(), dh3.getPublicKey());
 +assert.equal(dh1.getPrivateKey(), dh3.getPrivateKey());
 +
 +var secret3 = dh3.computeSecret(key2, 'hex', 'base64');
 +
 +assert.equal(secret1, secret3);
++
++
+ // Test RSA key signing/verification
+ var rsaSign = crypto.createSign('RSA-SHA1');
+ var rsaVerify = crypto.createVerify('RSA-SHA1');
+ assert.ok(rsaSign);
+ assert.ok(rsaVerify);
+ rsaSign.update(rsaPubPem);
+ var rsaSignature = rsaSign.sign(rsaKeyPem, 'hex');
+ assert.equal(rsaSignature, '5c50e3145c4e2497aadb0eabc83b342d0b0021ece0d4c4a064b7c8f020d7e2688b122bfb54c724ac9ee169f83f66d2fe90abeb95e8e1290e7e177152a4de3d944cf7d4883114a20ed0f78e70e25ef0f60f06b858e6af42a2f276ede95bbc6bc9a9bbdda15bd663186a6f40819a7af19e577bb2efa5e579a1f5ce8a0d4ca8b8f6');
+ rsaVerify.update(rsaPubPem);
+ assert.equal(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), 1);