index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. /**
  11. * A utility for performing bitwise operations on data.
  12. */
  13. module.exports = {
  14. /**
  15. * Concatenates a list of bytes objects, in the order that they are provided
  16. * as input.
  17. *
  18. * @function concatenate
  19. * @param {...Uint8Array}
  20. * arguments A comma separated list of the bytes objects to
  21. * concatenate, in the order that they are provided.
  22. * @returns {Uint8Array} The concatenation of the bytes objects.
  23. */
  24. concatenate: function() {
  25. var arrays = Array.prototype.slice.call(arguments);
  26. var totalLength = 0;
  27. for (var i = 0; i < arrays.length; i++) {
  28. totalLength += arrays[i].length;
  29. }
  30. var concatenated = new Uint8Array(totalLength);
  31. var offset = 0;
  32. for (var j = 0; j < arrays.length; j++) {
  33. concatenated.set(arrays[j], offset);
  34. offset += arrays[j].length;
  35. }
  36. return concatenated;
  37. },
  38. /**
  39. * Slices a subset of bytes from a concatenation of bytes.
  40. *
  41. * @function slice
  42. * @param {Uint8Array}
  43. * concatenated The concatenation of bytes.
  44. * @param {number}
  45. * start The index in the concatenation at which to start the
  46. * slice (inclusive).
  47. * @param {number}
  48. * [end] The index in the concatenation at which to end the slice
  49. * (exclusive). If no end index is supplied then all elements
  50. * from the start index to the end of the concatenation will be
  51. * sliced.
  52. * @returns {Uint8Array} The sliced subset of bytes.
  53. */
  54. slice: function(concatenated, start, end) {
  55. var concatenatedArray = Array.apply([], concatenated);
  56. var slicedArray = concatenatedArray.slice(start, end);
  57. return new Uint8Array(slicedArray);
  58. },
  59. /**
  60. * Prepends a byte to some existing bytes.
  61. *
  62. * @function prepend
  63. * @param {byte}
  64. * byte The byte to prepend.
  65. * @param {Uint8Array}
  66. * bytes The existing bytes.
  67. * @returns {Uint8Array} The bytes after prepending.
  68. */
  69. prepend: function(byte, bytes) {
  70. var prepended = new Uint8Array(1 + bytes.length);
  71. prepended.set(new Uint8Array([byte]));
  72. prepended.set(bytes, 1);
  73. return prepended;
  74. },
  75. /**
  76. * Appends a byte to some existing bytes.
  77. *
  78. * @function append
  79. * @param {Uint8Array}
  80. * bytes The existing bytes.
  81. * @param {byte}
  82. * byte The byte to append.
  83. * @returns {Uint8Array} The bytes after appending.
  84. */
  85. append: function(bytes, byte) {
  86. var appended = new Uint8Array(bytes.length + 1);
  87. appended.set(bytes);
  88. appended.set(new Uint8Array([byte]), bytes.length);
  89. return appended;
  90. }
  91. };