Fletcher8Checksum.mjs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /**
  2. * @author n1474335 [n1474335@gmail.com]
  3. * @copyright Crown Copyright 2016
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation.mjs";
  7. import Utils from "../Utils.mjs";
  8. /**
  9. * Fletcher-8 Checksum operation
  10. */
  11. class Fletcher8Checksum extends Operation {
  12. /**
  13. * Fletcher8Checksum constructor
  14. */
  15. constructor() {
  16. super();
  17. this.name = "Fletcher-8 Checksum";
  18. this.module = "Crypto";
  19. this.description = "The Fletcher checksum is an algorithm for computing a position-dependent checksum devised by John Gould Fletcher at Lawrence Livermore Labs in the late 1970s.<br><br>The objective of the Fletcher checksum was to provide error-detection properties approaching those of a cyclic redundancy check but with the lower computational effort associated with summation techniques.";
  20. this.infoURL = "https://wikipedia.org/wiki/Fletcher%27s_checksum";
  21. this.inputType = "ArrayBuffer";
  22. this.outputType = "string";
  23. this.args = [];
  24. }
  25. /**
  26. * @param {ArrayBuffer} input
  27. * @param {Object[]} args
  28. * @returns {string}
  29. */
  30. run(input, args) {
  31. let a = 0,
  32. b = 0;
  33. input = new Uint8Array(input);
  34. for (let i = 0; i < input.length; i++) {
  35. a = (a + input[i]) % 0xf;
  36. b = (b + a) % 0xf;
  37. }
  38. return Utils.hex(((b << 4) | a) >>> 0, 2);
  39. }
  40. }
  41. export default Fletcher8Checksum;