Skip to content

Commit

Permalink
Merge pull request #184 from Netflix/var-to-let
Browse files Browse the repository at this point in the history
Use let instead of var for block scope variables.
  • Loading branch information
wmiaw authored Sep 22, 2017
2 parents d5db4e7 + 8c62f9f commit 74b6a8d
Show file tree
Hide file tree
Showing 20 changed files with 380 additions and 421 deletions.
2 changes: 1 addition & 1 deletion core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"license": "Apache-2.0",
"scripts": {
"build": "npm run all",
"lint": "find -E src/main/javascript -type f -name '*.js' ! -regex '.*/(jsrsasign|clarinet|promise).js' | xargs jshint",
"lint": "find -E src/main/javascript -type f -name '*.js' ! -regex '.*/(jsrsasign|clarinet|promise).js' | xargs jshint --verbose",
"version": "echo `git describe --dirty`",
"jsdoc": "rm -rf ./jsdoc && jsdoc ./* -c ./jsdoc_conf.json || echo $?"
},
Expand Down
12 changes: 8 additions & 4 deletions core/src/main/javascript/crypto/JsonWebKeyAlgorithm.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,33 @@
* none.
*/
var JsonWebKeyAlgorithm$fromWebCryptoAlgorithm = function JsonWebKeyAlgorithm$fromWebCryptoAlgorithm(wcAlgo, nCryptoKey) {
var rawkey;

// We must compare by name because Web Crypto algorithm objects are not
// strictly defined.
switch (wcAlgo['name']) {
case WebCryptoAlgorithm.HMAC_SHA256['name']:
{
if (wcAlgo['hash'] && wcAlgo['hash']['name'] == WebCryptoAlgorithm.HMAC_SHA256['hash']['name'])
return JsonWebKeyAlgorithm.HS256;
return null;
}
case WebCryptoAlgorithm.RSASSA['name']:
return JsonWebKeyAlgorithm.RSA_15;
case WebCryptoAlgorithm.RSA_OAEP['name']:
return JsonWebKeyAlgorithm.RSA_OAEP;
case WebCryptoAlgorithm.A128KW['name']:
rawkey = nCryptoKey.rawkey;
{
let rawkey = nCryptoKey.rawkey;
if (rawkey.length == 128/8)
return JsonWebKeyAlgorithm.A128KW;
return null;
}
case WebCryptoAlgorithm.AES_CBC['name']:
rawkey = nCryptoKey.rawkey;
{
let rawkey = nCryptoKey.rawkey;
if (rawkey.length == 128/8)
return JsonWebKeyAlgorithm.A128CBC;
return null;
}
default:
return null;
}
Expand Down
6 changes: 2 additions & 4 deletions core/src/main/javascript/crypto/MslCiphertextEnvelope.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,12 @@
*/
var MslCiphertextEnvelope$parse = function MslCiphertextEnvelope$parse(mo, version, callback) {
AsyncExecutor(callback, function() {
var v;

// If a version was not specified, determine the envelope version.
if (!version) {
try {
version = mo.getInt(KEY_VERSION);
var identified = false;
for (v in Version) {
for (let v in Version) {
if (Version[v] == version) {
identified = true;
break;
Expand Down Expand Up @@ -255,7 +253,7 @@
case Version.V2:
try {
// Version 2 envelopes use the cipher specification.
v = mo.getInt(KEY_VERSION);
let v = mo.getInt(KEY_VERSION);
if (v != Version.V2)
throw new MslCryptoException(MslError.UNIDENTIFIED_CIPHERTEXT_ENVELOPE, "ciphertext envelope " + mo);
keyIdOrSpec = MslConstants.CipherSpec.fromString(mo.getString(KEY_CIPHERSPEC));
Expand Down
18 changes: 7 additions & 11 deletions core/src/main/javascript/crypto/MslCrypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,13 @@
},

'importKey': function(format, keyData, algorithm, extractable, keyUsage) {
var op;

var ext = normalizeExtractable(extractable);
var ku = normalizeKeyUsage(keyUsage);
switch (mslCrypto$version) {
case WebCryptoVersion.LEGACY:
case WebCryptoVersion.V2014_01:
case WebCryptoVersion.V2014_02:
op = nfCryptoSubtle.importKey(format, keyData, algorithm, ext, ku);
let op = nfCryptoSubtle.importKey(format, keyData, algorithm, ext, ku);
return promisedOperation(op);
case WebCryptoVersion.V2014_02_SAFARI:
if (format == KeyFormat.SPKI || format == KeyFormat.PKCS8) {
Expand All @@ -282,29 +280,27 @@
throw new Error("Could not make valid JWK from DER input");
}
var jwk = JSON.stringify(jwkObj);
op = nfCryptoSubtle.importKey(KeyFormat.JWK, textEncoding.getBytes(jwk), algorithm, ext, ku);
let op = nfCryptoSubtle.importKey(KeyFormat.JWK, textEncoding.getBytes(jwk), algorithm, ext, ku);
return promisedOperation(op);
} else {
op = nfCryptoSubtle.importKey(format, keyData, algorithm, ext, ku);
return promisedOperation(op);
let op = nfCryptoSubtle.importKey(format, keyData, algorithm, ext, ku);
return promisedOperation(op);
}
default:
throw new Error("Unsupported Web Crypto version " + mslCrypto$version + ".");
}
},

'exportKey': function(format, key) {
var op;

switch (mslCrypto$version) {
case WebCryptoVersion.LEGACY:
case WebCryptoVersion.V2014_01:
case WebCryptoVersion.V2014_02:
op = nfCryptoSubtle.exportKey(format, key);
let op = nfCryptoSubtle.exportKey(format, key);
return promisedOperation(op);
case WebCryptoVersion.V2014_02_SAFARI:
if (format == KeyFormat.SPKI || format == KeyFormat.PKCS8) {
op = nfCryptoSubtle.exportKey(KeyFormat.JWK, key);
let op = nfCryptoSubtle.exportKey(KeyFormat.JWK, key);
return promisedOperation(op).then(function (result) {
var jwkObj = JSON.parse(textEncoding.getString(new Uint8Array(result)));
var rsaKey = ASN1.jwkToRsaDer(jwkObj);
Expand All @@ -314,7 +310,7 @@
return rsaKey.getDer().buffer;
});
} else {
op = nfCryptoSubtle.exportKey(format, key);
let op = nfCryptoSubtle.exportKey(format, key);
return promisedOperation(op);
}
default:
Expand Down
18 changes: 8 additions & 10 deletions core/src/main/javascript/crypto/MslSignatureEnvelope.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,30 +202,27 @@
*/
var MslSignatureEnvelope$parse = function MslSignatureEnvelope$parse(envelope, version, encoder, callback) {
AsyncExecutor(callback, function() {
var envelopeMo, algorithm, signature;
var v;

if (version) {
switch (version) {
case Version.V1:
return new MslSignatureEnvelope(Version.V1, null, envelope);
case Version.V2:
try {
// We expect the byte representation to be a MSL object.
envelopeMo = encoder.parseObject(envelope);
let envelopeMo = encoder.parseObject(envelope);

// Verify version.
v = envelopeMo.getInt(KEY_VERSION);
let v = envelopeMo.getInt(KEY_VERSION);
if (Version.V2 != v)
throw new MslCryptoException(MslError.UNSUPPORTED_SIGNATURE_ENVELOPE, "signature envelope " + envelope);

// Grab algorithm.
algorithm = MslConstants.SignatureAlgo.fromString(envelopeMo.getString(KEY_ALGORITHM));
let algorithm = MslConstants.SignatureAlgo.fromString(envelopeMo.getString(KEY_ALGORITHM));
if (!algorithm)
throw new MslCryptoException(MslError.UNIDENTIFIED_ALGORITHM, "signature envelope " + envelope);

// Grab signature.
signature = envelopeMo.getBytes(KEY_SIGNATURE);
let signature = envelopeMo.getBytes(KEY_SIGNATURE);

// Return the envelope.
return new MslSignatureEnvelope(Version.V2, algorithm, signature);
Expand All @@ -240,6 +237,7 @@
}

// Attempt to convert this to a MSL object.
var envelopeMo;
try {
// If this is a MSL object, we expect the byte representation to be
// decodable.
Expand Down Expand Up @@ -267,7 +265,7 @@
envelopeVersion = Version.V1;
}
var recognized = false;
for (v in Version) {
for (let v in Version) {
if (Version[v] == envelopeVersion) {
recognized = true;
break;
Expand All @@ -291,8 +289,8 @@
return new MslSignatureEnvelope(envelopeVersion, null, envelope);
case Version.V2:
try {
algorithm = MslConstants.SignatureAlgo.fromString(envelopeMo.getString(KEY_ALGORITHM));
signature = envelopeMo.getBytes(KEY_SIGNATURE);
let algorithm = MslConstants.SignatureAlgo.fromString(envelopeMo.getString(KEY_ALGORITHM));
let signature = envelopeMo.getBytes(KEY_SIGNATURE);

// Verify algorithm.
if (!algorithm) {
Expand Down
141 changes: 70 additions & 71 deletions core/src/main/javascript/io/JsonMslArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
if (source instanceof Array) {
ja = source;
} else if (source instanceof MslArray) {
for (var i = 0; i < source.size(); ++i)
for (let i = 0; i < source.size(); ++i)
ja.push(source.opt(i));
} else if (source instanceof Uint8Array) {
try {
Expand All @@ -79,8 +79,8 @@

// Shallow copy the source data into this MSL array.
try {
for (var j = 0; j < ja.length; ++j)
this.put(-1, ja[j]);
for (let i = 0; i < ja.length; ++i)
this.put(-1, ja[i]);
} catch (e) {
if (e instanceof TypeError)
throw new MslEncoderException("Invalid MSL array encoding.", e);
Expand Down Expand Up @@ -177,75 +177,74 @@

if (i >= size)
return ja;

var jsonValue;

var value = this.opt(i);
if (value instanceof Uint8Array) {
ja.push(Base64.encode(value));
next(ja, size, i+1);
} else if (value instanceof JsonMslObject) {
value.toJSONObject(encoder, {
result: function(o) {
AsyncExecutor(callback, function() {
ja.push(o);
next(ja, size, i+1);
}, self);
},
error: callback.error,
});
} else if (value instanceof JsonMslArray) {
value.toJSONArray(encoder, {
result: function(a) {
AsyncExecutor(callback, function() {
ja.push(a);
next(ja, size, i+1);
}, self);
},
error: callback.error,
});
} else if (value instanceof MslObject) {
jsonValue = new JsonMslObject(encoder, value);
jsonValue.toJSONObject(encoder, {
result: function(o) {
AsyncExecutor(callback, function() {
ja.push(o);
next(ja, size, i+1);
}, self);
},
error: callback.error,
});
} else if (value instanceof MslArray) {
jsonValue = new JsonMslArray(encoder, value);
jsonValue.toJSONArray(encoder, {
result: function(a) {
AsyncExecutor(callback, function() {
ja.push(a);
next(ja, size, i+1);
}, self);
},
error: callback.error,
});
} else if (value instanceof MslEncodable) {
value.toMslEncoding(encoder, MslEncoderFormat.JSON, {
result: function(json) {
AsyncExecutor(callback, function() {
var jsonValue = new JsonMslObject(encoder, json);
jsonValue.toJSONObject(encoder, {
result: function(o) {
AsyncExecutor(callback, function() {
ja.push(o);
next(ja, size, i+1);
}, self);
},
error: callback.error,
});
}, self);
},
error: callback.error,
});
} else {
ja.push(value);
next(ja, size, i+1);
if (value instanceof Uint8Array) {
ja.push(Base64.encode(value));
next(ja, size, i+1);
} else if (value instanceof JsonMslObject) {
value.toJSONObject(encoder, {
result: function(o) {
AsyncExecutor(callback, function() {
ja.push(o);
next(ja, size, i+1);
}, self);
},
error: callback.error,
});
} else if (value instanceof JsonMslArray) {
value.toJSONArray(encoder, {
result: function(a) {
AsyncExecutor(callback, function() {
ja.push(a);
next(ja, size, i+1);
}, self);
},
error: callback.error,
});
} else if (value instanceof MslObject) {
let jsonValue = new JsonMslObject(encoder, value);
jsonValue.toJSONObject(encoder, {
result: function(o) {
AsyncExecutor(callback, function() {
ja.push(o);
next(ja, size, i+1);
}, self);
},
error: callback.error,
});
} else if (value instanceof MslArray) {
let jsonValue = new JsonMslArray(encoder, value);
jsonValue.toJSONArray(encoder, {
result: function(a) {
AsyncExecutor(callback, function() {
ja.push(a);
next(ja, size, i+1);
}, self);
},
error: callback.error,
});
} else if (value instanceof MslEncodable) {
value.toMslEncoding(encoder, MslEncoderFormat.JSON, {
result: function(json) {
AsyncExecutor(callback, function() {
var jsonValue = new JsonMslObject(encoder, json);
jsonValue.toJSONObject(encoder, {
result: function(o) {
AsyncExecutor(callback, function() {
ja.push(o);
next(ja, size, i+1);
}, self);
},
error: callback.error,
});
}, self);
},
error: callback.error,
});
} else {
ja.push(value);
next(ja, size, i+1);
}
}, self);
}
Expand Down
Loading

0 comments on commit 74b6a8d

Please sign in to comment.