[6a3a178] | 1 | /**
|
---|
| 2 | * Javascript implementation of mask generation function MGF1.
|
---|
| 3 | *
|
---|
| 4 | * @author Stefan Siegl
|
---|
| 5 | * @author Dave Longley
|
---|
| 6 | *
|
---|
| 7 | * Copyright (c) 2012 Stefan Siegl <stesie@brokenpipe.de>
|
---|
| 8 | * Copyright (c) 2014 Digital Bazaar, Inc.
|
---|
| 9 | */
|
---|
| 10 | var forge = require('./forge');
|
---|
| 11 | require('./util');
|
---|
| 12 |
|
---|
| 13 | forge.mgf = forge.mgf || {};
|
---|
| 14 | var mgf1 = module.exports = forge.mgf.mgf1 = forge.mgf1 = forge.mgf1 || {};
|
---|
| 15 |
|
---|
| 16 | /**
|
---|
| 17 | * Creates a MGF1 mask generation function object.
|
---|
| 18 | *
|
---|
| 19 | * @param md the message digest API to use (eg: forge.md.sha1.create()).
|
---|
| 20 | *
|
---|
| 21 | * @return a mask generation function object.
|
---|
| 22 | */
|
---|
| 23 | mgf1.create = function(md) {
|
---|
| 24 | var mgf = {
|
---|
| 25 | /**
|
---|
| 26 | * Generate mask of specified length.
|
---|
| 27 | *
|
---|
| 28 | * @param {String} seed The seed for mask generation.
|
---|
| 29 | * @param maskLen Number of bytes to generate.
|
---|
| 30 | * @return {String} The generated mask.
|
---|
| 31 | */
|
---|
| 32 | generate: function(seed, maskLen) {
|
---|
| 33 | /* 2. Let T be the empty octet string. */
|
---|
| 34 | var t = new forge.util.ByteBuffer();
|
---|
| 35 |
|
---|
| 36 | /* 3. For counter from 0 to ceil(maskLen / hLen), do the following: */
|
---|
| 37 | var len = Math.ceil(maskLen / md.digestLength);
|
---|
| 38 | for(var i = 0; i < len; i++) {
|
---|
| 39 | /* a. Convert counter to an octet string C of length 4 octets */
|
---|
| 40 | var c = new forge.util.ByteBuffer();
|
---|
| 41 | c.putInt32(i);
|
---|
| 42 |
|
---|
| 43 | /* b. Concatenate the hash of the seed mgfSeed and C to the octet
|
---|
| 44 | * string T: */
|
---|
| 45 | md.start();
|
---|
| 46 | md.update(seed + c.getBytes());
|
---|
| 47 | t.putBuffer(md.digest());
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | /* Output the leading maskLen octets of T as the octet string mask. */
|
---|
| 51 | t.truncate(t.length() - maskLen);
|
---|
| 52 | return t.getBytes();
|
---|
| 53 | }
|
---|
| 54 | };
|
---|
| 55 |
|
---|
| 56 | return mgf;
|
---|
| 57 | };
|
---|