You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
750 B

4 years ago
  1. var MAX_ALLOC = Math.pow(2, 30) - 1 // default in iojs
  2. function checkBuffer (buf, name) {
  3. if (typeof buf !== 'string' && !Buffer.isBuffer(buf)) {
  4. throw new TypeError(name + ' must be a buffer or string')
  5. }
  6. }
  7. module.exports = function (password, salt, iterations, keylen) {
  8. checkBuffer(password, 'Password')
  9. checkBuffer(salt, 'Salt')
  10. if (typeof iterations !== 'number') {
  11. throw new TypeError('Iterations not a number')
  12. }
  13. if (iterations < 0) {
  14. throw new TypeError('Bad iterations')
  15. }
  16. if (typeof keylen !== 'number') {
  17. throw new TypeError('Key length not a number')
  18. }
  19. if (keylen < 0 || keylen > MAX_ALLOC || keylen !== keylen) { /* eslint no-self-compare: 0 */
  20. throw new TypeError('Bad key length')
  21. }
  22. }