sign.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * Created by bmf on 11/2/13.
  3. *
  4. * Documentation of crypto http://nacl.cr.yp.to/box.html
  5. */
  6. /* jslint node: true */
  7. 'use strict';
  8. var binding = require('../build/Release/sodium');
  9. var assert = require('assert');
  10. var SignKey = require('./keys/sign-key');
  11. var toBuffer = require('./toBuffer');
  12. /**
  13. * Public-key authenticated message signatures: Sign
  14. *
  15. * @param {String|Buffer|Array} secretKey sender's private key.
  16. * @param {String|Buffer|Array} publicKey recipient's private key.
  17. *
  18. * @see Keys
  19. * @constructor
  20. */
  21. function Sign(key) {
  22. var self = this;
  23. /** default encoding to use in all string operations */
  24. self.defaultEncoding = undefined;
  25. if( key instanceof SignKey) {
  26. self.iKey = key;
  27. }
  28. else {
  29. /** Set of keys used to encrypt and decrypt messages */
  30. self.iKey = new SignKey();
  31. }
  32. /** Size of the generated message signature */
  33. self.bytes = function() {
  34. return binding.crypto_sign_BYTES;
  35. };
  36. /** String name of the default crypto primitive used in sign operations */
  37. self.primitive = function() {
  38. return binding.crypto_sign_PRIMITIVE;
  39. };
  40. /**
  41. * Get the keypair object
  42. * @returns {SignKey|*}
  43. */
  44. self.key = function() {
  45. return self.iKey;
  46. };
  47. /**
  48. * @return {Number} The size of the message signature
  49. */
  50. self.size = function() {
  51. return binding.crypto_sign_BYTES;
  52. };
  53. /**
  54. * Set the default encoding to use in all string conversions
  55. * @param {String} encoding encoding to use
  56. */
  57. self.setEncoding = function(encoding) {
  58. assert(!!encoding.match(/^(?:utf8|ascii|binary|hex|utf16le|ucs2|base64)$/), 'Encoding ' + encoding + ' is currently unsupported.');
  59. self.defaultEncoding = encoding;
  60. };
  61. /**
  62. * Get the current default encoding
  63. * @returns {undefined|String}
  64. */
  65. self.getEncoding = function() {
  66. return self.defaultEncoding;
  67. };
  68. /**
  69. * Digitally sign message
  70. *
  71. * @param {Buffer|String|Array} message message to sign
  72. * @param {String} [encoding] encoding of message string
  73. *
  74. * @returns {Object} cipher box
  75. */
  76. self.sign = function (message, encoding) {
  77. encoding = String(encoding) || self.defaultEncoding || 'utf8';
  78. var buf = toBuffer(message, encoding);
  79. var signature = binding.crypto_sign(buf, self.iKey.sk().get());
  80. if( !signature ) {
  81. return undefined;
  82. }
  83. return {
  84. sign: signature,
  85. publicKey: self.iKey.pk().get()
  86. };
  87. };
  88. /**
  89. * Digitally sign message, using detached signature
  90. *
  91. * @param {Buffer|String|Array} message message to sign
  92. * @param {String} [encoding] encoding of message string
  93. *
  94. * @returns {Object} cipher box
  95. */
  96. self.signDetached = function (message, encoding) {
  97. encoding = String(encoding) || self.defaultEncoding || 'utf8';
  98. var buf = toBuffer(message, encoding);
  99. var signature = binding.crypto_sign_detached(buf, self.iKey.sk().get());
  100. if( !signature ) {
  101. return undefined;
  102. }
  103. return {
  104. sign: signature,
  105. publicKey: self.iKey.pk().get()
  106. };
  107. };
  108. }
  109. /**
  110. * Verify digital signature
  111. *
  112. * @param {Buffer|String|Array} cipherText the signed message
  113. */
  114. Sign.verify = function (signature) {
  115. assert(typeof signature == 'object' && signature.hasOwnProperty('sign') && signature.hasOwnProperty('publicKey'));
  116. return binding.crypto_sign_open(signature.sign, signature.publicKey);
  117. };
  118. /**
  119. * Verify digital signature (detached mode)
  120. *
  121. * @param {Buffer|String|Array} signature the signature
  122. * @param {Buffer|String|Array} message the message
  123. *
  124. * returns true if verified successfully, false otherwise.
  125. */
  126. Sign.verifyDetached = function (signature, message) {
  127. assert(typeof signature == 'object' && signature.hasOwnProperty('sign') && signature.hasOwnProperty('publicKey'));
  128. return binding.crypto_sign_verify_detached(signature.sign, message, signature.publicKey);
  129. };
  130. module.exports = Sign;