Add example to sha1 docs
authorRyan Dahl <ry@tinyclouds.org>
Sun, 3 Apr 2011 08:09:00 +0000 (01:09 -0700)
committerRyan Dahl <ry@tinyclouds.org>
Sun, 3 Apr 2011 08:09:00 +0000 (01:09 -0700)
doc/api/crypto.markdown

index 702a9c3..78284b3 100644 (file)
@@ -29,6 +29,24 @@ which can be used to generate hash digests.
 of OpenSSL on the platform. Examples are `'sha1'`, `'md5'`, `'sha256'`, `'sha512'`, etc.
 On recent releases, `openssl list-message-digest-algorithms` will display the available digest algorithms.
 
+Example: this program that takes the sha1 sum of a file
+
+    var filename = process.argv[2];
+    var crypto = require('crypto');
+    var fs = require('fs');
+
+    var shasum = crypto.createHash('sha1');
+
+    var s = fs.ReadStream(filename);
+    s.on('data', function(d) {
+      shasum.update(d);
+    });
+
+    s.on('end', function() {
+      var d = shasum.digest('hex');
+      console.log(d + '  ' + filename);
+    });
+
 ### hash.update(data)
 
 Updates the hash content with the given `data`.