/** * Copyright 2013 the PM2 project authors. All rights reserved. * Use of this source code is governed by a license that * can be found in the LICENSE file. */ var crypto = require('crypto'); var saltChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; var saltCharsCount = saltChars.length; function generateSalt(len) { if (typeof len != 'number' || len <= 0 || len !== parseInt(len, 10)) throw new Error('Invalid salt length'); if (crypto.randomBytes) { return crypto.randomBytes(Math.ceil(len / 2)).toString('hex').substring(0, len); } else { for (var i = 0, salt = ''; i < len; i++) { salt += saltChars.charAt(Math.floor(Math.random() * saltCharsCount)); } return salt; } } function generateHash(algorithm, salt, password, iterations) { iterations = iterations || 1; try { var hash = password; for(var i=0; i