<commit_before>#!/usr/bin/env node

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*

 Usage:
 scripts/gen_keys.js

 Will create these files

 ./config/key.json
 ./config/oldKey.json

 If these files already exist, this script will show an error message
 and exit. You must remove both keys if you want to generate a new
 keypair.
 */

const fs = require('fs');
const assert = require('assert');
const generateRSAKeypair = require('keypair');
const JwTool = require('fxa-jwtool');

const keyPath = './config/key.json';
const oldKeyPath = './config/oldKey.json';

try {
  var keysExist = fs.existsSync(keyPath) && fs.existsSync(oldKeyPath);
  assert(!keysExist, 'keys already exists');
} catch (e) {
  process.exit();
}

function main(cb) {
  var privKey = JwTool.JWK.fromPEM(generateRSAKeypair().private, {
    kid: '2015.12.16-1'
  });

  try {
    fs.mkdirSync('./config');
  } catch (accessEx) {

  }

  fs.writeFileSync(keyPath, JSON.stringify(privKey.toJSON(), undefined, 2));
  console.log('Key saved:', keyPath); //eslint-disable-line no-console

  // The "old key" is not used to sign anything, so we don't need to store
  // the private component, we just need to serve the public component
  // so that old signatures can be verified correctly.
  var pubKey = JwTool.JWK.fromPEM(generateRSAKeypair().public, {
    kid: '2015.12.16-2'
  });
  fs.writeFileSync(oldKeyPath, JSON.stringify(pubKey.toJSON(), undefined, 2));
  console.log('OldKey saved:', oldKeyPath); //eslint-disable-line no-console
  cb();
}

module.exports = main;

if (require.main === module) {
  main(function () {
  });
}<commit_msg>fix(keys): Generate unique 'kid' field when regenerating JWK keys<commit_after>#!/usr/bin/env node

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*

 Usage:
 scripts/gen_keys.js

 Will create these files

 ./config/key.json
 ./config/oldKey.json

 If these files already exist, this script will show an error message
 and exit. You must remove both keys if you want to generate a new
 keypair.
 */

const fs = require('fs');
const assert = require('assert');
const crypto = require('crypto');
const generateRSAKeypair = require('keypair');
const JwTool = require('fxa-jwtool');

const keyPath = './config/key.json';
const oldKeyPath = './config/oldKey.json';

try {
  var keysExist = fs.existsSync(keyPath) && fs.existsSync(oldKeyPath);
  assert(!keysExist, 'keys already exists');
} catch (e) {
  process.exit();
}

// Generate a unique key id using hash of public key, and timestamp.
// This comes out like "2017-01-23-ebe69008de771d62cd1cadf9faa6daae".
function makeKeyId(kp) {
  return [
    (new Date()).toISOString().slice(0, 10) + '-' +
    crypto.createHash('sha256').update(kp.public).digest('hex').slice(0, 32)
  ].join('-');
}

function main(cb) {
  var kp = generateRSAKeypair();
  var privKey = JwTool.JWK.fromPEM(kp.private, {
    kid: makeKeyId(kp)
  });

  try {
    fs.mkdirSync('./config');
  } catch (accessEx) {

  }

  fs.writeFileSync(keyPath, JSON.stringify(privKey.toJSON(), undefined, 2));
  console.log('Key saved:', keyPath); //eslint-disable-line no-console

  // The "old key" is not used to sign anything, so we don't need to store
  // the private component, we just need to serve the public component
  // so that old signatures can be verified correctly.
  kp = generateRSAKeypair();
  var pubKey = JwTool.JWK.fromPEM(kp.public, {
    kid: makeKeyId(kp)
  });
  fs.writeFileSync(oldKeyPath, JSON.stringify(pubKey.toJSON(), undefined, 2));
  console.log('OldKey saved:', oldKeyPath); //eslint-disable-line no-console
  cb();
}

module.exports = main;

if (require.main === module) {
  main(function () {
  });
}
{'fraction_non_alphanumeric': 0.1443661971830986, 'fraction_numerical': 0.015090543259557344, 'mean_word_length': 3.7345238095238096, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1313906', 'n_tokens_mistral': 1393, 'n_tokens_neox': 1295, 'n_words': 394}