adobe / reactor-extension-core

This is the Core extension for Adobe Experience Platform Tags. It provides default event, condition, action, and data element types to all Tags properties.
https://experienceleague.adobe.com/docs/experience-platform/tags/extensions/adobe/core/overview.html
Apache License 2.0
20 stars 13 forks source link

Create a data element t hash-256 #79

Open alcazes opened 1 year ago

alcazes commented 1 year ago

At the moment there is no official data element from the core extension to allow to hash value.

We have a rely on third party code or third party extension to hash value using sha-256

This is the code that the Adobe Visitor ID service use, it will be good to have a data element feature create in the core extension to support this

function e(t) {
            function n(e, t) {
                return e >>> t | e << 32 - t
            }
            for (var i, r, a = Math.pow, o = a(2, 32), s = "", c = [], u = 8 * t.length, l = e.h = e.h || [], d = e.k = e.k || [], f = d.length, p = {}, g = 2; f < 64; g++)
                if (!p[g]) {
                    for (i = 0; i < 313; i += g) p[i] = g;
                    l[f] = a(g, .5) * o | 0, d[f++] = a(g, 1 / 3) * o | 0
                } for (t += "€"; t.length % 64 - 56;) t += "\0";
            for (i = 0; i < t.length; i++) {
                if ((r = t.charCodeAt(i)) >> 8) return;
                c[i >> 2] |= r << (3 - i) % 4 * 8
            }
            for (c[c.length] = u / o | 0, c[c.length] = u, r = 0; r < c.length;) {
                var m = c.slice(r, r += 16),
                    h = l;
                for (l = l.slice(0, 8), i = 0; i < 64; i++) {
                    var _ = m[i - 15],
                        C = m[i - 2],
                        S = l[0],
                        I = l[4],
                        v = l[7] + (n(I, 6) ^ n(I, 11) ^ n(I, 25)) + (I & l[5] ^ ~I & l[6]) + d[i] + (m[i] = i < 16 ? m[i] : m[i - 16] + (n(_, 7) ^ n(_, 18) ^ _ >>> 3) + m[i - 7] + (n(C, 17) ^ n(C, 19) ^ C >>> 10) | 0);
                    l = [v + ((n(S, 2) ^ n(S, 13) ^ n(S, 22)) + (S & l[1] ^ S & l[2] ^ l[1] & l[2])) | 0].concat(l), l[4] = l[4] + v | 0
                }
                for (i = 0; i < 8; i++) l[i] = l[i] + h[i] | 0
            }
            for (i = 0; i < 8; i++)
                for (r = 3; r + 1; r--) {
                    var D = l[i] >> 8 * r & 255;
                    s += (D < 16 ? 0 : "") + D.toString(16)
                }
            return s
        }
jeffreywalter commented 1 year ago

@alcazes Do the third party extensions that provide this functionality not provide value? Is there a reason that the other extensions are undesirable?

We have to balance the amount of "weight" we put into the core extension vs how much functionality a specific delegate would provide the widest audience.

alcazes commented 1 year ago

In our business we decided not to use any third party extension other than official Adobe Ones. We always check if an extension is hosted on the official Adobe Github to validate it is maintained by Adobe. (So any Adobe Consulting one not hosted on Adobe repo will not be trusted and used). This decision was taken after we had 2 incidents with third party extensions.

At present I am writing my own private extensions but I feel hashing value should come out of the box from the Tag core extension.

As far as I understand the builds will not add any data element code if the data element type was never configured in the first place, so only people using this functionality will see this the hash function code in their adobe launch js library.

jeffreywalter commented 1 year ago

Thanks for the context. We will engage with product to see where this would fall for them. We'll respond back here when we have a response for you!

mems commented 6 months ago

You can use native version crypto.subtle.digest(), but it's async and be aware it's disabled on insecure origins (cf. window.isSecureContext):

async function digestMessage(message) {
  const msgUint8 = new TextEncoder().encode(message); // encode as (utf-8) Uint8Array
  const hashBuffer = await crypto.subtle.digest("SHA-256", msgUint8); // hash the message
  const hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array
  const hashHex = hashArray
    .map((b) => b.toString(16).padStart(2, "0"))
    .join(""); // convert bytes to hex string
  return hashHex;
}

console.log(await digestMessage("An obscure body in the S-K System, your majesty. The inhabitants refer to it as the planet Earth."));// > "6efd383745a964768989b9df420811abc6e5873f874fc22a76fe9258e020c2e1"