const asn1js = require("asn1js");

const base64Str = "MIICqgIBATALBgkqhkiG9w0BAQUxcJ8/DNMRDGlQARKk+XplpJ9ABOEQbwCfSxStCi2gbCSR3oN8os/lQ0ct1Siqxp+HbQcBNCMAUoiIn5c9DAAAAADu7u7u7u7u75+XPgQAAAAAn5c/BAEAAACfl0AEAQAAAJ+XQQQBAAAAn5dMBAAAAAAEggEADqUqa3wHYM1pzGZBJjSx2P9BixVITDKeDSFMy2sYXoMvD1hXKdZuxHQn6k/s9o4isF6xRFO3fBJW6znxdhD62bw1TKBur/3wmRSeMGNwaZEab2Be41DHMhNDTM5zlB4Iw5ykqRTSkLI95hXObI0GS6UUEYi+H5+TX1YY1Ipi0zZwpU6P/U/rTJURiLQ1Hcm9pJeW4GzNa2m9ywBhcEv41wj8fY2ycYd2JN9ScMR++SRs8tWw8nxRJmvABDirh6JTcczQVWsDdNihM1yhQrOoCNvS6UDrta0GuPsjfHBN1LRk81m5pJp6nQrUlV3Tn1H++/Kn/Q5JRLd2EvE2FygbW6OCASAwCwYJKoZIhvcNAQEBA4IBDwAwggEKAoIBAQCskU9F2dz8TtWBq2D8AdsqcYS51H66DxZmCHEw6U9p3d8vjaEcBdF5VFwETmWJBcTJo/SiPLezdAmG40RfAsxg4sIok0CPhKsTp1mon0JBqai68SdmN0L+AsEbmNK4AjjMX6GM5t7w5mdXpgZyigRtGQDnV2P7HnOZj69PS9r/D4Q50CJNaLrGJZ1UVBNcKkJNTMD2pxrHnxdSLTj51xVITBU71Tdl7KghSskP8WagOONk6J0IcOCwIaWct9A/+Aso4yk5/PDh1YUhbUiIO+z1TL5TdiHLITgc8NXHagB/yiOEEzOx2pcZVXXjwfSZlKRHj66VlWVHgT+bEHZl0/sdAgMBAAE=";

// Convert base64 string to buffer
const bufferObj = Buffer.from(base64Str, "base64");

async function decodeData_parse() {
    // Parse ASN.1 structure
    const asn1 = asn1js.ASN1.fromBER(bufferObj);
    const ret = asn1.result.valueBlock;

    const {
        INTEGER,
        OID,
        PRIMITIVES,
        OCTET_STRING,
        CONSTRUCTED_BIT_STRING,
        CONSTRUCTED_OID,
    } = parseActivationTicket(ret);

    console.log({
        OCTET_STRING,
        BASE64: Buffer.from(OCTET_STRING).toString("base64"),
        HEX: Buffer.from(OCTET_STRING).toString("hex"),
    });

    const SET$ = [];
    let IMEI = null;
    let MEID = false;

    for (const [i, item] of PRIMITIVES.entries()) {
        if (item.tagNumber == 1003 || item.tagNumber == 1007) {
            MEID = true;
        }

        if (item.tagNumber == 1005) {
            let hexString = "";
            for (let i = 0; i < item.valueHex.length; i++) {
                const hexValue = item.valueHex[i].toString(16);
                hexString += hexValue.padStart(2, "0");
            }
            IMEI = hexString;
        }

        SET$.push(
            new asn1js.Primitive({
                idBlock: {
                    tagClass: item.idBlock.tagClass,
                    tagNumber: item.idBlock.tagNumber,
                },
                valueHex: item.valueHex,
            })
        );
    }

    const mountDefaultSequence = {
        name: "ConstructedValueBlock",
        value: [
            new asn1js.Integer({ valueHex: INTEGER }),
            new asn1js.Sequence({
                value: [
                    new asn1js.ObjectIdentifier({ value: OID }),
                ],
            }),
            new asn1js.Set({ value: SET$ }),
            new asn1js.OctetString({ value: OCTET_STRING }),
            new asn1js.Constructed({
                identifieForm: true,
                idBlock: {
                    tagClass: 1,
                    tagNumber: 1,
                },
                value: [
                    new asn1js.Sequence({
                        value: [
                            new asn1js.ObjectIdentifier({ value: CONSTRUCTED_OID }),
                        ],
                    }),
                    new asn1js.BitString({ isHexOnly: true, valueHex: CONSTRUCTED_BIT_STRING }),
                ],
            }),
        ],
    };

    const ticketGSM = new asn1js.Sequence(mountDefaultSequence);
    const encoded = Buffer.from(ticketGSM.toBER()).toString("base64");

    const ticketMEID = new asn1js.Sequence(mountDefaultSequence);

    if (!MEID) {
        for (const [i, item] of ticketMEID.toJSON().valueBlock.value[2].valueBlock.value.entries()) {
            if (item.idBlock.tagNumber == 1005) {
                mountDefaultSequence.value[2].value.splice(
                    i + 1,
                    0,
                    new asn1js.Primitive({
                        idBlock: { tagClass: 1, tagNumber: 1007 },
                        valueHex: await payloadToUint8Array("35321810693143"),
                    })
                );
            }
        }
    }

    if (!MEID) {
        for (const [i, item] of ticketGSM.toJSON().valueBlock.value[2].valueBlock.value.entries()) {
            if (item.idBlock.tagNumber == 1005) {
                mountDefaultSequence.value[2].value.splice(i, 1);
            }
        }
    }

    if (!MEID) {
        for (const [i, item] of ticketMEID.toJSON().valueBlock.value[2].valueBlock.value.entries()) {
            if (item.idBlock.tagNumber == 1007) {
                mountDefaultSequence.value[2].value.splice(
                    i + 1,
                    0,
                    new asn1js.Primitive({
                        idBlock: { tagClass: 1, tagNumber: 1005 },
                        valueHex: await payloadToUint8Array("35321810693143"),
                    })
                );
            }
        }
    }

    const encoded_meid = Buffer.from(ticketMEID.toBER()).toString("base64");

    const TICKET = {
        OID,
        IMEI,
        POSSIBLE_MEID: MEID,
        VALIDATE_BUILD: base64Str === encoded,
        TICKET_ORIGINAL: base64Str,
        TICKET_BUILD: encoded,
        TICKET_MEID: !MEID ? encoded_meid : "TICKET_ORIGINAL AND TICKET_BUILD IS MEID",
    };
	
async function payloadToUint8Array(payload) {
    // Convert the payload to a Uint8Array
    const uint8Array = new TextEncoder().encode(payload);
    return uint8Array;
}
    console.log("ticket", ticketGSM.toString());
    console.log(TICKET);
}

async function parseActivationTicket(payload) {
    const INTEGER = payload.integerValue;
    const OID = payload.oidValue;
    const PRIMITIVES = payload.primitiveValues;
    const OCTET_STRING = payload.octetStringValue;
    const CONSTRUCTED_BIT_STRING = payload.constructedBitStringValue;
    const CONSTRUCTED_OID = payload.constructedOIDValue;

    return { INTEGER, OID, PRIMITIVES, OCTET_STRING, CONSTRUCTED_BIT_STRING, CONSTRUCTED_OID };
} 

NodeJS Online Compiler

Write, Run & Share NodeJS code online using OneCompiler's NodeJS online compiler for free. It's one of the robust, feature-rich online compilers for NodeJS language,running on the latest LTS version NodeJS 16.14.2. Getting started with the OneCompiler's NodeJS editor is easy and fast. The editor shows sample boilerplate code when you choose language as NodeJS and start coding. You can provide the dependencies in package.json.

About NodeJS

Node.js is a free and open-source server environment. Node.js is very popular in recent times and a large number of companies like Microsoft, Paypal, Uber, Yahoo, General Electric and many others are using Node.js.

Key features

  • Built on Google chrome's javascript engine V8 and is pretty fast.
  • Node.js was developed by Ryan Dahl in 2009.
  • Server-side platform for building fast and scalable applications.
  • Node.js is Asynchronous, event-driven and works on single-thread model thus eliminating the dis-advantages of multi-thread model.
  • Supports various platforms like Windows, Linux, MacOS etc.
  • Provides rich library of java script modules which simplifies the development efforts.
  • Released under MIT license.

Express Framework

Express is one of the most popular web application framework in the NodeJS echosystem.

  • Pretty fast
  • Minimalist
  • Unopinionated
  • Very flexible

Syntax help

Examples

Using Moment

let moment = require('moment');

console.log(moment().format('MMMM Do YYYY, h:mm:ss a'));

Using Lodash

const _ = require("lodash");

let colors = ['blue', 'green', 'yellow', 'red'];

let firstElement = _.first(colors);
let lastElement = _.last(colors);

console.log(`First element: ${firstElement}`);
console.log(`Last element: ${lastElement}`);

Libraries supported

Following are the libraries supported by OneCompiler's NodeJS compiler.

  • lodash
  • moment
  • underscore
  • uuid
  • ejs
  • md5
  • url