How to use the md function from node-forge

Find comprehensive JavaScript node-forge.md code examples handpicked from public code repositorys.

76
77
78
79
80
81
82
83
84
85
  * Calculates a MD5 hash.
  * @param {String} data to hash
  * @returns {String} last 8 characters of hash of data
  */
  md5(data) {
    const md5hash = forge.md.md5.create().update(data).digest().toHex();
    return md5hash.toString().toLowerCase().substr(8, 16);
  }
}
module.exports = TuyaCipher;
fork icon311
star icon1
watch icon0

76
77
78
79
80
81
82
83
84
85
  };
}

function generateCertsForHostname(domain, rootCAConfig) {
  // generate a serialNumber for domain
  const md = forge.md.md5.create();
  md.update(domain);

  const keysAndCert = getKeysAndCert(md.digest().toHex());
  const keys = keysAndCert.keys;
fork icon196
star icon0
watch icon30

+ 59 other calls in file

8
9
10
11
12
13
14
15
16
17
var os = require('os');
var https = require('https');
var path = require('path');
var forge = require('node-forge');
var pki = forge.pki;
var md5 = forge.md.md5;
var mkdirp = require('./mkdirp');
var dirtool = require('../helpers/dirTool');
var certDir = dirtool.getCertificateDir();
var defaultAttrs = [
fork icon59
star icon673
watch icon22

+ 27 other calls in file

74
75
76
77
78
79
80
81
82
83
    return cipher;
}

function md5(buffer)
{
    return forge.md.md5.create().update(buffer.bytes()).digest();
}

function deflate(data)
{
fork icon95
star icon7
watch icon0

+ 7 other calls in file

123
124
125
126
127
128
129
130
131
132
serializer = serializer || JSON.stringify
return asyncify(function (unencryptedValue) {
  var publicPEM = importPublicKey(publicJwk)
  var publicKey = forge.pki.publicKeyFromPem(publicPEM)
  var enc = publicKey.encrypt(forge.util.encodeUtf8(serializer(unencryptedValue)), 'RSA-OAEP', {
    md: forge.md.sha256.create()
  })
  var result = cipher.serialize(
    Unibabel.binaryStringToBuffer(enc), null, ASYMMETRIC_ALGO_RSA_OAEP
  )
fork icon38
star icon706
watch icon6

+ 11 other calls in file

133
134
135
136
137
138
139
140
141
142
let attrs = [{name: 'commonName', value: 'DevtoolsProServer'}];

cert.setSubject(attrs);
cert.setIssuer(attrs);
cert.setExtensions(SERVER_EXTENSIONS);
cert.sign(keys.privateKey, NodeForge.md.sha256.create());
const certPem = pki.certificateToPem(cert);
const keyPem = pki.privateKeyToPem(keys.privateKey);
// 创建
await mkdirp(dir);
fork icon23
star icon155
watch icon5

+ 3 other calls in file

62
63
64
65
66
67
68
69
70
switch (messageDigest) {
  case 'sha1':
    return forge.md.sha1.create();

  case 'sha256':
    return forge.md.sha256.create();

  case 'sha384':
    return forge.md.sha384.create();
fork icon40
star icon126
watch icon6

+ 23 other calls in file

208
209
210
211
212
213
214
215
216
217
    // check ! > 0 so that it fails for undefined
    if (!(iterationCount > 0)) {
        throw new Error('Invalid iterationCount: ' + iterationCount);
    }
    return forge.pkcs5.pbkdf2(
        password, salt, iterationCount, _PBE_AES_KEY_BYTES, forge.md.sha1.create());
}

var PBE_DECRYPT_FAILED_MESSAGE = 'AES decryption failed (password incorrect or data is corrupt?)';
function _decryptKey(keyString, password) {
fork icon17
star icon38
watch icon9

+ 3 other calls in file

2
3
4
5
6
7
8
9
10
/**
 * @param text [String]
 * @return hash [String] e.g. 21232f297a57a5a743894a0e4a801fc3
 */
module.exports = function md5(text) {
  var md = forge.md.md5.create();
  md.update(text, 'utf8');
  return md.digest().toHex();
};
fork icon2
star icon6
watch icon0

+ 13 other calls in file

15
16
17
18
19
20
21
22
23
24
        let i=0, ret=[];
        while(i++<2) await create(864e8).then(({key,cert})=>{
                cert.setSubject([{shortName:'CN', value:issuer}]);
                cert.setIssuer([{shortName:'CN', value:issuer}]);
                cert.setExtensions([{name:'basicConstraints', critical:true, cA:true}]);
                cert.sign(key, forge.md.sha256.create());
                ret.push({key:pki.privateKeyToPem(key), cert:pki.certificateToPem(cert)});
        });
        return ret;
}
fork icon7
star icon12
watch icon2

+ 7 other calls in file

12
13
14
15
16
17
18
19
20
21
// Notes:
// 1. If using an alternative hash (eg: "-md sha1") pass
//   "forge.md.sha1.create()" as the final parameter.
// 2. If using "-nosalt", set salt to null.
var salt = forge.random.getBytesSync(8);
// var md = forge.md.sha1.create(); // "-md sha1"
var derivedBytes = forge.pbe.opensslDeriveBytes(
  password, salt, keySize + ivSize/*, md*/);
var buffer = forge.util.createBuffer(derivedBytes);
var key = buffer.getBytes(keySize);
fork icon1
star icon1
watch icon3

57
58
59
60
61
62
63
64
65
66

cert.setSubject(attrs)
cert.setIssuer(attrs)

cert.publicKey = forge.pki.publicKeyFromPem(keypair.public)
cert.sign(forge.pki.privateKeyFromPem(keypair.private), forge.md.sha256.create())
let certPem = forge.pki.certificateToPem(cert)

try {
  fs.unlinkSync(keyFile)
fork icon0
star icon1
watch icon2

108
109
110
111
112
113
114
115
116
117
    cert.validity.notBefore = new Date(Date.now()-1*date.ms.DAY);
    cert.validity.notAfter = new Date(Date.now()+1*365*date.ms.DAY);
    cert.setSubject([{name: 'commonName', value: name}]);
    cert.setIssuer(pki.certificateFromPem(E.ca.cert).issuer.attributes);
    cert.setExtensions([{name: 'subjectAltName', altNames: alt_names}]);
    cert.sign(pki.privateKeyFromPem(E.ca.key), forge.md.sha256.create());
    return cert;
}

function ssl(keys, extra_ssl_ips){
fork icon187
star icon616
watch icon53

+ 5 other calls in file

94
95
96
97
98
99
100
101
102
103
if (supportRSA()) {
        getRSAKey(false, ({ pemPvt }: Object) => {
                const privateKey = forge.pki.privateKeyFromPem(pemPvt);
                const decoded64 = forge.util.decode64(encrypted);
                const token = privateKey.decrypt(decoded64, 'RSA-OAEP', {
                        md: forge.md.sha256.create(),
                });
                onSuccess(token);
        });
}
fork icon5
star icon13
watch icon7

+ 3 other calls in file

44
45
46
47
48
49
50
51
52
 * @param {Object} keys - keys to sign with
 * @return {string} signature
 */
static sign(message, keys) {
    // sign payload with the provided private key
    const md = forge.md.sha256.create();
    md.update(message, 'utf8');
    return base64ToBase64Url(forge.util.encode64(keys.privateKey.sign(md)));
}
fork icon5
star icon13
watch icon14

+ 3 other calls in file

202
203
204
205
206
207
208
209
210
    name: 'subjectKeyIdentifier'
  }]);


  // self-sign certificate
  cert.sign(keys.private /*, forge.md.sha256.create()*/);
  debug('Certificate created');
  return cert;
}
fork icon5
star icon4
watch icon5

392
393
394
395
396
397
398
399
400
401
// 加密和解密,使用 RSAES-OAEP/SHA-256/MGF1-SHA-1
// 兼容 Java 的 RSA/ECB/OAEPWithSHA-256AndMGF1Padding
var encrypted = publicKey.encrypt(bytes, 'RSA-OAEP', {
  md: forge.md.sha256.create(),
  mgf1: {
    md: forge.md.sha1.create(),
  },
});
var decrypted = privateKey.decrypt(encrypted, 'RSA-OAEP', {
  md: forge.md.sha256.create(),
fork icon1
star icon3
watch icon2

+ 17 other calls in file

28
29
30
31
32
33
34
35
36
37
// Make the (request-target) value
const reqt = `${http_method} ${endpoint}`;
const date = new Date();
const created = parseInt(date.getTime() / 1000, 10);
// Make the body digest
const md = forge.md.sha512.create();
const parsed = query ? JSON.stringify(query) : '';
md.update(parsed);
// Finalise the digest
const sha = forge.util.encode64(md.digest().data);
fork icon0
star icon4
watch icon0

30
31
32
33
34
35
36
37
38
39
    name: 'commonName',
    value: hostname
}]);

cert.setIssuer(ROOT_CRT.subject.attributes);
cert.sign(ROOT_KEY, forge.md.sha256.create());
cert = {
    key: pki.privateKeyToPem(ROOT_KEY),
    cert: pki.certificateToPem(cert)
};
fork icon2
star icon1
watch icon3

1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
            {
                shortName: "O",
                value: "Amazon",
            },
        ]);
        csr.sign(forge.pki.privateKeyFromPem(keys.privateKey), forge.md.sha256.create());
        this.mqttdata.key = forge.pki.certificationRequestToPem(csr);
    }
    this.log.info("Create certification done");
}
fork icon3
star icon7
watch icon1

+ 3 other calls in file