ip2dword

This ip2dword tag converts an IPv4 address (in string format) into a 32-bit number by processing each octet and adjusting based on its position. It optionally multiplies the result by a repetition factor (repeat).

Created by: hackvertor
Installed 1 times

Category: IP

Created on: Tuesday, October 22, 2024 at 11:38:01 AM

Updated on: Wednesday, November 6, 2024 at 3:20:40 PM

This is a built in tag
Tag arguments
[
   {
      "type": "number",
      "help": "This provides the number of times to repeat the dword encoding",
      "defaultValue": "1"
   }
]
Code
class ip2dword {
  encode(input, repeat) {
    const segments = input.split(".").map(Number);
    let result = 0;
    const qty = 256 ** segments.length;

    if (segments.length % 2 === 1) {
      for (let i = 0; i < segments.length; i += 3) {
        result = result * 256 + segments[i];
        if (segments[i + 1] !== undefined)
          result = result * 256 + segments[i + 1];
        if (segments[i + 2] !== undefined)
          result = result * 256 + segments[i + 2];
      }
    } else {
      for (let i = 0; i < segments.length; i += 2) {
        if (i % 4 === 2) result *= 256;
        result = result * 256 + segments[i];
        result = result * 256 + segments[i + 1];
      }
    }

    return result + qty * repeat;
  }
}