How do I Encode my score value with JWT

0 favourites
  • 2 posts
From the Asset Store
This Student Workbook uses both Construct 3 & 2 encoding. It supplies client-side & php "back-end" encoding.
  • Hello there,

    i had a similar topic, but i want to ask you now for a more specific area.

    So i have to encode my score value with JSON Web Token library. I want to use the Crypto-js Library, because it uses the HS256 Algorithm i would need. I downloaded it on github : https://github.com/brix/crypto-js

    My question is how to achieve this. If i have a library - can i place it in the root folder of my project, and access this folder with a javascript, i created in construct3?

    When the player wants to submit his score, i request a secret from a URL, which is needet too synchronize the data. This secret has to be encoded by the library function too. So basically there are a score value and the secret value.

    • Now i have the following javascript in my project - this will (should) execute the process:

    var header = {

    "alg": "HS256",

    "typ": "JWT"

    };

    var data = {

    "id": 1337,

    "score": "254"

    };

    var secret = "My very confidential secret!!!";

    function base64url(source) {

    // Encode in classical base64

    encodedSource = CryptoJS.enc.Base64.stringify(source);

    // Remove padding equal characters

    encodedSource = encodedSource.replace(/=+$/, '');

    // Replace characters according to base64url specifications

    encodedSource = encodedSource.replace(/\+/g, '-');

    encodedSource = encodedSource.replace(/\//g, '_');

    return encodedSource;

    }

    var stringifiedHeader = CryptoJS.enc.Utf8.parse(JSON.stringify(header));

    var encodedHeader = base64url(stringifiedHeader);

    var stringifiedData = CryptoJS.enc.Utf8.parse(JSON.stringify(data));

    var encodedData = base64url(stringifiedData);

    var signature = encodedHeader + "." + encodedData;

    signature = CryptoJS.HmacSHA256(signature, secret);

    signature = base64url(signature);

    From the Library i took the enc-base64 script, i thought it would be the script which is needed to encode the values. The question is how to combine two extern javascripts with each other. Or if basically the complete library is still needed to access...

    • So the score and the secret are the only variables i have in construct, and i would have to complete in this javascript (Thats the first question, how can i use the command to set those values into the javascript).

    -The second question: The Javascript calls the CryptoJS.enc.Utf8 library, how can i setup the connection to a library, which isnt included in the project? Can i access it, when it is in the project folder? ( or in the root of the project)?

    And i would have to receive the encrypted value/string, which would look like this:

    eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MTMzNywic2NvcmUiOiIyNTQifQ.xSv1wylmcO5VW5_bbWxrTmvnX9IaTbWWQbDNxjyuSL4

    (Header:Payload:Security)

    It would be so important, if someone could tell me how to achieve this... or if its possible to do that with construct3 - its my first time to using a library, or setup a connection between c3 and javascripts and libraries, which are not included in construct3.

    Thank you so much for your help!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • This is the encoding script (the second one i included into the project)... the question is, if this two scripts are the only one i would need to encode values - but it seems to be so:

    (function () {

    // Shortcuts

    var C = CryptoJS;

    var C_lib = C.lib;

    var WordArray = C_lib.WordArray;

    var C_enc = C.enc;

    /**

    * Base64 encoding strategy.

    */

    var Base64 = C_enc.Base64 = {

    /**

    * Converts a word array to a Base64 string.

    *

    * param {WordArray} wordArray The word array.

    *

    * {string} The Base64 string.

    *

    *

    *

    * ExampLe

    *

    * var base64String = CryptoJS.enc.Base64.stringify(wordArray);

    */

    stringify: function (wordArray) {

    // Shortcuts

    var words = wordArray.words;

    var sigBytes = wordArray.sigBytes;

    var map = this._map;

    // Clamp excess bits

    wordArray.clamp();

    // Convert

    var base64Chars = [];

    for (var i = 0; i < sigBytes; i += 3) {

    var byte1 = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;

    var byte2 = (words[(i + 1) >>> 2] >>> (24 - ((i + 1) % 4) * 8)) & 0xff;

    var byte3 = (words[(i + 2) >>> 2] >>> (24 - ((i + 2) % 4) * 8)) & 0xff;

    var triplet = (byte1 << 16) | (byte2 << 8) | byte3;

    for (var j = 0; (j < 4) && (i + j * 0.75 < sigBytes); j++) {

    base64Chars.push(map.charAt((triplet >>> (6 * (3 - j))) & 0x3f));

    }

    }

    // Add padding

    var paddingChar = map.charAt(64);

    if (paddingChar) {

    while (base64Chars.length % 4) {

    base64Chars.push(paddingChar);

    }

    }

    return base64Chars.join('');

    },

    /**

    * Converts a Base64 string to a word array.

    *

    * param {string} base64Str The Base64 string.

    *

    * {WordArray} The word array.

    *

    *

    *

    * ExampLe

    *

    * var wordArray = CryptoJS.enc.Base64.parse(base64String);

    */

    parse: function (base64Str) {

    // Shortcuts

    var base64StrLength = base64Str.length;

    var map = this._map;

    var reverseMap = this._reverseMap;

    if (!reverseMap) {

    reverseMap = this._reverseMap = [];

    for (var j = 0; j < map.length; j++) {

    reverseMap[map.charCodeAt(j)] = j;

    }

    }

    // Ignore padding

    var paddingChar = map.charAt(64);

    if (paddingChar) {

    var paddingIndex = base64Str.indexOf(paddingChar);

    if (paddingIndex !== -1) {

    base64StrLength = paddingIndex;

    }

    }

    // Convert

    return parseLoop(base64Str, base64StrLength, reverseMap);

    },

    _map: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='

    };

    function parseLoop(base64Str, base64StrLength, reverseMap) {

    var words = [];

    var nBytes = 0;

    for (var i = 0; i < base64StrLength; i++) {

    if (i % 4) {

    var bits1 = reverseMap[base64Str.charCodeAt(i - 1)] << ((i % 4) * 2);

    var bits2 = reverseMap[base64Str.charCodeAt(i)] >>> (6 - (i % 4) * 2);

    words[nBytes >>> 2] |= (bits1 | bits2) << (24 - (nBytes % 4) * 8);

    nBytes++;

    }

    }

    return WordArray.create(words, nBytes);

    }

    }());

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)