utils-test.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. 'use strict';
  2. var assert = require('assert');
  3. var des = require('../');
  4. var utils = des.utils;
  5. var fixtures = require('./fixtures');
  6. var bin = fixtures.bin;
  7. describe('utils', function() {
  8. describe('IP', function() {
  9. it('should permute properly', function() {
  10. var out = new Array(2);
  11. var inp = [
  12. bin('00000001 00100011 01000101 01100111'),
  13. bin('10001001 10101011 11001101 11101111')
  14. ];
  15. utils.ip(inp[0], inp[1], out, 0);
  16. var expected = [
  17. bin('11001100 00000000 11001100 11111111'),
  18. bin('11110000 10101010 11110000 10101010')
  19. ];
  20. assert.deepEqual(out, expected);
  21. });
  22. it('should rev-permute properly', function() {
  23. var out = new Array(2);
  24. var inp = [
  25. bin('11001100 00000000 11001100 11111111'),
  26. bin('11110000 10101010 11110000 10101010')
  27. ];
  28. utils.rip(inp[0], inp[1], out, 0);
  29. var expected = [
  30. bin('00000001 00100011 01000101 01100111'),
  31. bin('10001001 10101011 11001101 11101111')
  32. ];
  33. assert.deepEqual(out, expected);
  34. });
  35. });
  36. describe('PC1', function() {
  37. it('should permute properly', function() {
  38. var out = new Array(2);
  39. var inp = [
  40. bin('00010011 00110100 01010111 01111001'),
  41. bin('10011011 10111100 11011111 11110001')
  42. ];
  43. utils.pc1(inp[0], inp[1], out, 0);
  44. var expected = [
  45. bin('1111000 0110011 0010101 0101111'),
  46. bin('0101010 1011001 1001111 0001111')
  47. ];
  48. assert.deepEqual(out, expected);
  49. });
  50. });
  51. describe('r28shl', function() {
  52. it('should shl properly', function() {
  53. assert.equal(utils.r28shl(bin('1111000011001100101010101111'), 1),
  54. bin('1110000110011001010101011111'));
  55. assert.equal(utils.r28shl(bin('0101010101100110011110001111'), 1),
  56. bin('1010101011001100111100011110'));
  57. assert.equal(utils.r28shl(bin('1111000011001100101010101111'), 4),
  58. bin('0000110011001010101011111111'));
  59. assert.equal(utils.r28shl(bin('0101010101100110011110001111'), 4),
  60. bin('0101011001100111100011110101'));
  61. });
  62. });
  63. describe('PC2', function() {
  64. it('should permute properly', function() {
  65. var out = new Array(2);
  66. var inp = [
  67. bin('1110000 1100110 0101010 1011111'),
  68. bin('1010101 0110011 0011110 0011110')
  69. ];
  70. utils.pc2(inp[0], inp[1], out, 0);
  71. var expected = [
  72. bin('000110 110000 001011 101111'),
  73. bin('111111 000111 000001 110010')
  74. ];
  75. assert.deepEqual(out, expected);
  76. });
  77. });
  78. describe('readUInt32BE', function() {
  79. it('should read number properly', function() {
  80. var a = [ 0xde, 0xad, 0xbe, 0xef ];
  81. var o = utils.readUInt32BE(a, 0);
  82. assert.equal(o, 0xdeadbeef);
  83. });
  84. });
  85. describe('writeUInt32BE', function() {
  86. it('should read number properly', function() {
  87. var a = [ 0, 0, 0, 0 ];
  88. utils.writeUInt32BE(a, 0xdeadbeef, 0);
  89. var expected = [ 0xde, 0xad, 0xbe, 0xef ];
  90. assert.deepEqual(a, expected);
  91. });
  92. });
  93. describe('expand', function() {
  94. it('should expand', function() {
  95. var out = [ 0, 0 ];
  96. utils.expand(bin('1111 0000 1010 1010 1111 0000 1010 1010'), out, 0);
  97. var expected = [
  98. bin('011110 100001 010101 010101'),
  99. bin('011110 100001 010101 010101')
  100. ];
  101. assert.deepEqual(out, expected);
  102. });
  103. it('should expand with low 1', function() {
  104. var out = [ 0, 0 ];
  105. utils.expand(bin('1111 0000 1010 1010 1111 0000 1010 1011'), out, 0);
  106. var expected = [
  107. bin('111110 100001 010101 010101'),
  108. bin('011110 100001 010101 010111')
  109. ];
  110. assert.deepEqual(out, expected);
  111. });
  112. it('should expand with low 1', function() {
  113. var out = [ 0, 0 ];
  114. utils.expand(bin('10100010 01011100 00001011 11110100'), out, 0);
  115. var expected = [
  116. bin('010100 000100 001011 111000'),
  117. bin('000001 010111 111110 101001')
  118. ];
  119. assert.deepEqual(out, expected);
  120. });
  121. });
  122. describe('substitute', function() {
  123. it('should substitute', function() {
  124. var input = [
  125. bin('011000 010001 011110 111010'),
  126. bin('100001 100110 010100 100111')
  127. ];
  128. var output = utils.substitute(input[0], input[1]);
  129. assert.equal(output, bin('0101 1100 1000 0010 1011 0101 1001 0111'));
  130. });
  131. });
  132. describe('permute', function() {
  133. it('should permute', function() {
  134. var output = utils.permute(
  135. bin('0101 1100 1000 0010 1011 0101 1001 0111'));
  136. assert.equal(output, bin('0010 0011 0100 1010 1010 1001 1011 1011'));
  137. });
  138. });
  139. });