Index: frog/tests/frog/src/NodeCryptoTest.dart |
=================================================================== |
--- frog/tests/frog/src/NodeCryptoTest.dart (revision 0) |
+++ frog/tests/frog/src/NodeCryptoTest.dart (revision 0) |
@@ -0,0 +1,168 @@ |
+// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+ |
+#library('NodeCryptoTest'); |
+ |
+#import('unittest_node.dart'); |
+#import('../../../lib/node/node.dart'); |
+#import('../../../lib/node/crypto.dart'); |
+ |
+void main() { |
+ test('Credentials', () { |
+ Credentials credentials = crypto.createCredentials(); |
+ }); |
+ |
+ test('Hash', () { |
+ { |
+ Hash hash = crypto.createHash('md5'); |
+ hash.update("The quick brown fox jumps over the lazy dog"); |
+ String digest = hash.digest('hex'); |
+ Expect.equals('9e107d9d372bb6826bd81d3542a419d6', digest); |
+ } |
+ { |
+ Hash hash = crypto.createHash('md5'); |
+ hash.updateBuffer(new Buffer.fromString( |
+ "The quick brown fox jumps over the lazy dog")); |
+ String digest = hash.digest('hex'); |
+ Expect.equals('9e107d9d372bb6826bd81d3542a419d6', digest); |
+ } |
+ }); |
+ |
+ test('Hmac', () { |
+ { |
+ Hmac hmac = crypto.createHmac('sha1', '1234567'); |
+ hmac.update("hello world"); |
+ String digest = hmac.digest('base64'); |
+ Expect.equals('c6jmoNeWbPMlfL48Wm7HzwanzaQ=', digest); |
+ } |
+ { |
+ Hmac hmac = crypto.createHmac('sha1', '1234567'); |
+ hmac.updateBuffer(new Buffer.fromString("hello world")); |
+ String digest = hmac.digest('base64'); |
+ Expect.equals('c6jmoNeWbPMlfL48Wm7HzwanzaQ=', digest); |
+ } |
+ }); |
+ |
+ test('Cipher / Decipher', () { |
+ String algorithm = 'aes-256-cbc'; |
+ String password = 'password'; |
+ String plaintext = 'This is a test.'; |
+ StringBuffer enciphered = new StringBuffer(); |
+ { |
+ Cipher cipher = crypto.createCipher(algorithm, password); |
+ enciphered.add(cipher.update(plaintext)); |
+ enciphered.add(cipher.finalData()); |
+ } |
+ StringBuffer deciphered = new StringBuffer(); |
+ { |
+ Decipher decipher = crypto.createDecipher(algorithm, password); |
+ decipher.update(enciphered.toString()); |
+ deciphered.add(decipher.finalData()); |
+ } |
+ Expect.equals(plaintext, deciphered.toString()); |
+ }); |
+ |
+ test('Signer / Verifier', () { |
+ // Dummy certificate created with: openssl req -x509 -nodes \ |
+ // -days 36500 -subj '/C=US/ST=California/L=MountainView/CN=www.example.com' \ |
+ // -newkey rsa:1024 -keyout mykey.pem -out mycert.pem |
+ |
+ String privateKey = |
+""" |
+-----BEGIN PRIVATE KEY----- |
+MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKUnAuAX/cMhPm8O |
+Q5cLiXqR/15sZ38ab+jqi2+aRP63pTbA1Utpl9nd5IM+Sgf2HJ94uqSyxV3jGBBH |
+HBxQ4Y/+LL2S6VnKolEStnlP5lQa/yKRnvLFUX+4qIgdyXln+iFNmO6R/OgdHfBb |
+TVr7883hLavjnrTVeRgske2Zxw+ZAgMBAAECgYAacyUCvtTla22UW7R3fGGIP9mm |
+RbZNpO5HX0j1lr92C/Np0XhXm5G2UtNGMbOpksECyVMbDYaOgtBXywu1fT6ijzNL |
+if/c4vUW+OC6agV59Wl+tX/AnNK7xDO/i0aWklnOrUmHG73wrSMLdvJyLeVkK3jx |
+j3QbWcuLgH0E87BQAQJBANFARIPxCQEvfCItvu1ULAMNYwWFIFecPkj0YKXuYYeR |
+YDcZUaDQBoi1aI9HI06eJMYLNELMhcr8NT/0C8h5dg0CQQDKDJnnquEbvegccZ+W |
+FfZEWQ/2SOR2mh6JFNfh2kSJYCTROab1vDAjiX3ME0NgRZw09g1G7TzALF3ypx2B |
+9Ii9AkEAmR4CBNpX0HpCx2/aCihRnForX1qu8+zs1s2b+0+YJm+GjEsGpDoUzeyQ |
++mb/uwOVvSVttIOcU5CCFq4qASR/8QJAZgslM67G0CcCclMkYT2oSe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xvdznub1aoDHmr/cV4OoIUEfyX2IT4KgF |
+4/bg0eByfq9n7Q== |
+-----END CERTIFICATE----- |
+"""; |
+ |
+ String document = 'I am a document.'; |
+ Signer signer = crypto.createSign('RSA-SHA256'); |
+ signer.update(document); |
+ String signature = signer.sign(privateKey); |
+ |
+ Verifier verifier = crypto.createVerify('RSA-SHA256'); |
+ verifier.update(document); |
+ Expect.equals(true, verifier.verify(cert, signature)); |
+ }); |
+ |
+ test('DiffieHellman', () { |
+ DiffieHellman server = crypto.createDiffieHellman(512); |
+ String prime = server.getPrime(); |
+ // sharing secret key on a pair |
+ DiffieHellman alice = crypto.createDiffieHellmanFromPrime(prime); |
+ alice.generateKeys(); |
+ String alicePub = alice.getPublicKey(); |
+ |
+ DiffieHellman bob = crypto.createDiffieHellmanFromPrime(prime); |
+ bob.generateKeys(); |
+ String bobPub = bob.getPublicKey(); |
+ |
+ String bobAliceSecret = bob.computeSecret(alicePub); |
+ String aliceBobSecret = alice.computeSecret(bobPub); |
+ |
+ // public keys are different, but secret is common. |
+ Expect.equals(false, bobPub == alicePub); |
+ Expect.equals(true, bobAliceSecret == aliceBobSecret); |
+ }); |
+ |
+ asyncTest('pbkdf2', 1, () { |
+ String password = 'password'; |
+ String salt = 'salt'; |
+ int iterations = 5; |
+ int keylen = 256; |
+ crypto.pbkdf2(password, salt, iterations, keylen, |
+ (Error err, String derivedKey1) { |
+ crypto.pbkdf2(password, salt, iterations, keylen, |
+ (Error err, String derivedKey2) { |
+ Expect.equals(derivedKey1, derivedKey2); |
+ callbackDone(); |
+ }); |
+ }); |
+ }); |
+ |
+ group('rand', () { |
+ test('sync', (){ |
+ SlowBuffer buf = crypto.randomBytes(12); |
+ Expect.equals(12, buf.length); |
+ }); |
+ asyncTest('async', 1, (){ |
+ crypto.randomBytes(12, (Error err, SlowBuffer buf) { |
+ Expect.equals(12, buf.length); |
+ callbackDone(); |
+ }); |
+ }); |
+ }); |
+} |