1const crypto = require('crypto');
2
3const secret = 'abcdefg';
4const hash = crypto.createHmac('sha256', secret)
5 .update('I love cupcakes')
6 .digest('hex');
7console.log(hash);
8// Prints:
9// c0fa1bc00531bd78ef38c628449c5102aeabd49b5dc3a2a516ea6ea959d6658e
10
1require("crypto")
2 .createHash("sha256")
3 .update("Man oh man do I love node!")
4 .digest("hex");
5
1function HashTable(obj)
2{
3 this.length = 0;
4 this.items = {};
5 for (var p in obj) {
6 if (obj.hasOwnProperty(p)) {
7 this.items[p] = obj[p];
8 this.length++;
9 }
10 }
11
12 this.setItem = function(key, value)
13 {
14 var previous = undefined;
15 if (this.hasItem(key)) {
16 previous = this.items[key];
17 return previous;
18 }
19 else {
20 this.length++;
21 }
22 this.items[key] = value;
23 return previous;
24 }
25
26 this.getItem = function(key) {
27 return this.hasItem(key) ? this.items[key] : undefined;
28 }
29
30 this.hasItem = function(key)
31 {
32 return this.items.hasOwnProperty(key);
33 }
34
35 this.removeItem = function(key)
36 {
37 if (this.hasItem(key)) {
38 previous = this.items[key];
39 this.length--;
40 delete this.items[key];
41 return previous;
42 }
43 else {
44 return undefined;
45 }
46 }
47
48 this.keys = function()
49 {
50 var keys = [];
51 for (var k in this.items) {
52 if (this.hasItem(k)) {
53 keys.push(k);
54 }
55 }
56 return keys;
57 }
58
59 this.values = function()
60 {
61 var values = [];
62 for (var k in this.items) {
63 if (this.hasItem(k)) {
64 values.push(this.items[k]);
65 }
66 }
67 return values;
68 }
69
70 this.each = function(fn) {
71 for (var k in this.items) {
72 if (this.hasItem(k)) {
73 fn(k, this.items[k]);
74 }
75 }
76 }
77
78 this.clear = function()
79 {
80 this.items = {}
81 this.length = 0;
82 }
83}
84
1var h = new Object(); // or just {}
2h['one'] = 1;
3h['two'] = 2;
4h['three'] = 3;
5
6// show the values stored
7for (var k in h) {
8 // use hasOwnProperty to filter out keys from the Object.prototype
9 if (h.hasOwnProperty(k)) {
10 alert('key is: ' + k + ', value is: ' + h[k]);
11 }
12}
13