2
0

index.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright 2018 Scytl Secure Electronic Voting SA
  3. *
  4. * All rights reserved
  5. *
  6. * See our extended copyright notice in *file 'Copyright.txt' which is part of this source code package
  7. */
  8. /* jshint node:true */
  9. 'use strict';
  10. var forge = require('node-forge');
  11. /**
  12. * Codec for converting between various types of data.
  13. */
  14. module.exports = {
  15. /**
  16. * UTF-8 encodes a string.
  17. *
  18. * @function utf8Encode
  19. * @param {string}
  20. * str The string to UTF-8 encode.
  21. * @returns {Uint8Array} The UTF-8 encoded string.
  22. */
  23. utf8Encode: function(str) {
  24. return this.binaryDecode(forge.util.encodeUtf8(str));
  25. },
  26. /**
  27. * UTF-8 decodes an encoded string.
  28. *
  29. * @function utf8Decode
  30. * @param {Uint8Array}
  31. * encoded The encoded string to UTF-8 decode.
  32. * @returns {string} The UTF-8 decoded string.
  33. */
  34. utf8Decode: function(encoded) {
  35. return forge.util.decodeUtf8(this.binaryEncode(encoded));
  36. },
  37. /**
  38. * Converts a BigInteger object to bytes.
  39. *
  40. * @function bigIntegerToBytes
  41. * @param {forge.jsbn.BigInteger}
  42. * bigInt The BigInteger object to convert.
  43. * @returns {Uint8Array} The bytes from the conversion.
  44. */
  45. bigIntegerToBytes: function(bigInt) {
  46. return new Uint8Array(bigInt.toByteArray());
  47. },
  48. /**
  49. * Converts some bytes to a BigInteger object.
  50. *
  51. * @function bytesToBigInteger
  52. * @param {Uint8Array}
  53. * bytes The bytes to convert.
  54. * @returns {forge.jsbn.BigInteger} The BigInteger object from the
  55. * conversion.
  56. */
  57. bytesToBigInteger: function(bytes) {
  58. var hex = this.hexEncode(bytes);
  59. return new forge.jsbn.BigInteger(hex, 16);
  60. },
  61. /**
  62. * Base64 encodes some data.
  63. * <p>
  64. * <b>NOTE:</b> Input of type <code>string</code> will be UTF-8 encoded.
  65. *
  66. * @function base64Encode
  67. * @param {Uint8Array|string|BigInteger}
  68. * data The data to Base64 encode.
  69. * @param {Object}
  70. * [options] An object containing optional arguments.
  71. * @param {number}
  72. * [options.lineLength=No line breaks] The line length of the
  73. * result.
  74. * @returns {string} The Base64 encoded data.
  75. */
  76. base64Encode: function(data, options) {
  77. options = options || {};
  78. if (typeof data === 'string') {
  79. data = this.utf8Encode(data);
  80. } else if (!(data instanceof Uint8Array)) {
  81. data = this.bigIntegerToBytes(data);
  82. }
  83. if (typeof options.lineLength === 'undefined') {
  84. return forge.util.binary.base64.encode(data);
  85. } else {
  86. return forge.util.binary.base64.encode(data, options.lineLength);
  87. }
  88. },
  89. /**
  90. * Base64 decodes some encoded data.
  91. * <p>
  92. * <b>NOTE:</b> To retrieve data of type <code>string</code>, apply method
  93. * <code>utf8Decode</code> to result.
  94. * <p>
  95. * <b>NOTE:</b> To retrieve data of type <code>BigInteger</code>, apply method
  96. * <code>bytesToBigInteger</code> to result.
  97. *
  98. * @function base64Decode
  99. * @param {string}
  100. * baseB64 The encoded data to Base64 decode.
  101. * @returns {Uint8Array} The Base64 decoded data.
  102. */
  103. base64Decode: function(baseB64) {
  104. return new Uint8Array(forge.util.binary.base64.decode(baseB64));
  105. },
  106. /**
  107. * Hexadecimally encodes some data.
  108. * <p>
  109. * <b>NOTE:</b> Input of type <code>string</code> will be UTF-8 encoded.
  110. *
  111. * @function hexEncode
  112. * @param {Uint8Array|string|BigInteger}
  113. * data The data to hexadecimally encode.
  114. * @returns {string} The hexadecimally encoded data.
  115. */
  116. hexEncode: function(data) {
  117. if (data instanceof Uint8Array) {
  118. return forge.util.binary.hex.encode(data);
  119. } else if (typeof data === 'string') {
  120. return forge.util.binary.hex.encode(this.utf8Encode(data));
  121. } else {
  122. var bytes = this.bigIntegerToBytes(data);
  123. return forge.util.binary.hex.encode(bytes);
  124. }
  125. },
  126. /**
  127. * Hexadecimally decodes some encoded data.
  128. * <p>
  129. * <b>NOTE:</b> To retrieve data of type <code>string</code>, apply method
  130. * <code>utf8Decode</code> to result.
  131. * <p>
  132. * <b>NOTE:</b> To retrieve data of type <code>BigInteger</code>, apply method
  133. * <code>bytesToBigInteger</code> to result.
  134. *
  135. * @function hexDecode
  136. * @param {string}
  137. * hex The encoded data to hexadecimally decode.
  138. * @returns {Uint8Array} The hexadecimally decoded data.
  139. */
  140. hexDecode: function(hex) {
  141. return forge.util.binary.hex.decode(hex);
  142. },
  143. /**
  144. * Binary encodes some bytes. This method is only intended for internal use.
  145. *
  146. * @function binaryEncode
  147. * @deprecated Use Uint8Array objects instead of binary encodings to
  148. * represent bytes.
  149. * @private
  150. * @param {Uint8Array|byte[]}
  151. * byteArray The bytes to binary encode.
  152. * @returns {string} The binary encoded bytes.
  153. */
  154. binaryEncode: function(bytes) {
  155. var encoded = '';
  156. for (var i = 0; i < bytes.length; i++) {
  157. encoded += String.fromCharCode(bytes[i]);
  158. }
  159. return encoded;
  160. },
  161. /**
  162. * Binary decodes some encoded bytes. This method is only intended for
  163. * internal use.
  164. *
  165. * @function binaryDecode
  166. * @deprecated Use Uint8Array objects instead of binary encodings to
  167. * represent bytes.
  168. * @private
  169. * @param {string}
  170. * encoded The encoded bytes to binary decode.
  171. * @returns {Uint8Array} The binary decoded bytes.
  172. */
  173. binaryDecode: function(encoded) {
  174. var byteArray = [];
  175. for (var i = 0; i < encoded.length; i++) {
  176. byteArray.push(encoded.charCodeAt(i));
  177. }
  178. return new Uint8Array(byteArray);
  179. }
  180. };