stream.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* jslint node: true */
  2. 'use strict';
  3. var binding = require('../build/Release/sodium');
  4. var StreamKey = require('./keys/stream-key');
  5. var toBuffer = require('./toBuffer');
  6. var Nonce = require('./nonces/stream-nonce');
  7. var assert = require('assert');
  8. /**
  9. * @param {String|Buffer|Array} [secretKey] A valid stream secret key
  10. * @constructor
  11. */
  12. module.exports = function Stream(secretKey, encoding) {
  13. var self = this;
  14. /** default encoding to use in all string operations */
  15. self.defaultEncoding = undefined;
  16. // Init key
  17. self.secretKey = new StreamKey(secretKey, encoding);
  18. /** String name of the default crypto primitive used in stream operations */
  19. self.primitive = function() {
  20. return binding.crypto_stream_PRIMITIVE;
  21. };
  22. /**
  23. * Get the auth-key secret key object
  24. * @returns {AuthKey|*}
  25. */
  26. self.key = function() {
  27. return self.secretKey;
  28. };
  29. /**
  30. * Set the default encoding to use in all string conversions
  31. * @param {String} encoding encoding to use
  32. */
  33. self.setEncoding = function(encoding) {
  34. assert.equal(typeof encoding, 'string');
  35. assert.ok(encoding.match(/^(?:utf8|ascii|binary|hex|utf16le|ucs2|base64)$/));
  36. self.defaultEncoding = encoding;
  37. };
  38. /**
  39. * Get the current default encoding
  40. * @returns {undefined|String}
  41. */
  42. self.getEncoding = function() {
  43. return self.defaultEncoding;
  44. };
  45. self.generate = function(size, nonce, encoding) {
  46. assert.equal(typeof size, 'number');
  47. assert.ok(size > 0)
  48. var n = new Nonce(nonce, encoding);
  49. var stream = binding.crypto_stream(size, n.get(), self.secretKey.get());
  50. if( !stream ) {
  51. return undefined;
  52. }
  53. return {
  54. stream: stream,
  55. nonce : n.get()
  56. };
  57. };
  58. /**
  59. * Encrypt the message
  60. *
  61. * @param {string|Buffer|Array} message message to authenticate
  62. * @param {String} [encoding ] If v is a string you can specify the encoding
  63. */
  64. self.encrypt = function(message, encoding) {
  65. encoding = encoding || self.defaultEncoding || 'utf8';
  66. var messageBuf = toBuffer(message, encoding);
  67. var nonce = new Nonce();
  68. var cipherText = binding.crypto_stream_xor(messageBuf, nonce.get(), self.secretKey.get());
  69. if( !cipherText ) {
  70. return undefined;
  71. }
  72. return {
  73. cipherText: cipherText,
  74. nonce : nonce.get()
  75. };
  76. };
  77. /**
  78. * The decrypt function verifies and decrypts a cipherText using the
  79. * secret key and a nonce.
  80. * The function returns the resulting plaintext m.
  81. *
  82. * @param {Buffer|String|Array} cipherText the encrypted message
  83. * @param {Buffer|String|Array} nonce the nonce used to encrypt
  84. * @param {String} [encoding] the encoding to return the plainText
  85. */
  86. self.decrypt = function (cipherBox, encoding) {
  87. encoding = String(encoding || self.defaultEncoding || 'utf8');
  88. assert.equal(typeof cipherBox, 'object');
  89. assert.notEqual(typeof cipherBox.cipherText, 'undefined');
  90. assert.notEqual(typeof cipherBox.nonce, 'undefined');
  91. assert.ok(cipherBox.cipherText instanceof Buffer);
  92. assert.ok(cipherBox.nonce instanceof Buffer);
  93. var nonce = new Nonce(cipherBox.nonce);
  94. var plainText = binding.crypto_stream_xor(
  95. cipherBox.cipherText,
  96. nonce.get(),
  97. self.secretKey.get()
  98. );
  99. if( encoding ) {
  100. return plainText.toString(encoding);
  101. }
  102. return plainText;
  103. };
  104. };