BitwiseOp.mjs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * Bitwise operation resources.
  3. *
  4. * @author n1474335 [n1474335@gmail.com]
  5. * @copyright Crown Copyright 2018
  6. * @license Apache-2.0
  7. */
  8. /**
  9. * Runs bitwise operations across the input data.
  10. *
  11. * @param {byteArray|Uint8Array} input
  12. * @param {byteArray} key
  13. * @param {function} func - The bitwise calculation to carry out
  14. * @param {boolean} nullPreserving
  15. * @param {string} scheme
  16. * @returns {byteArray}
  17. */
  18. export function bitOp (input, key, func, nullPreserving, scheme) {
  19. if (!key || !key.length) key = [0];
  20. const result = [];
  21. let x = null,
  22. k = null,
  23. o = null;
  24. for (let i = 0; i < input.length; i++) {
  25. k = key[i % key.length];
  26. if (scheme === "Cascade") k = input[i + 1] || 0;
  27. o = input[i];
  28. x = nullPreserving && (o === 0 || o === k) ? o : func(o, k);
  29. result.push(x);
  30. if (scheme &&
  31. scheme !== "Standard" &&
  32. !(nullPreserving && (o === 0 || o === k))) {
  33. switch (scheme) {
  34. case "Input differential":
  35. key[i % key.length] = x;
  36. break;
  37. case "Output differential":
  38. key[i % key.length] = o;
  39. break;
  40. }
  41. }
  42. }
  43. return result;
  44. }
  45. /**
  46. * XOR bitwise calculation.
  47. *
  48. * @param {number} operand
  49. * @param {number} key
  50. * @returns {number}
  51. */
  52. export function xor(operand, key) {
  53. return operand ^ key;
  54. }
  55. /**
  56. * NOT bitwise calculation.
  57. *
  58. * @param {number} operand
  59. * @returns {number}
  60. */
  61. export function not(operand, _) {
  62. return ~operand & 0xff;
  63. }
  64. /**
  65. * AND bitwise calculation.
  66. *
  67. * @param {number} operand
  68. * @param {number} key
  69. * @returns {number}
  70. */
  71. export function and(operand, key) {
  72. return operand & key;
  73. }
  74. /**
  75. * OR bitwise calculation.
  76. *
  77. * @param {number} operand
  78. * @param {number} key
  79. * @returns {number}
  80. */
  81. export function or(operand, key) {
  82. return operand | key;
  83. }
  84. /**
  85. * ADD bitwise calculation.
  86. *
  87. * @param {number} operand
  88. * @param {number} key
  89. * @returns {number}
  90. */
  91. export function add(operand, key) {
  92. return (operand + key) % 256;
  93. }
  94. /**
  95. * SUB bitwise calculation.
  96. *
  97. * @param {number} operand
  98. * @param {number} key
  99. * @returns {number}
  100. */
  101. export function sub(operand, key) {
  102. const result = operand - key;
  103. return (result < 0) ? 256 + result : result;
  104. }
  105. /**
  106. * Delimiter options for bitwise operations
  107. */
  108. export const BITWISE_OP_DELIMS = ["Hex", "Decimal", "Binary", "Base64", "UTF8", "Latin1"];