CB Hash - MD5, SHA-1 and SHA-256 hashing algorithms.

4

Stats

7,259 visits, 11,555 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 3 Aug, 2011. Last updated 19 Feb, 2019

CornedBeef Hash

CB Hash is a plugin for Construct 2 allowing the user to hash a string and some other parameters, depending on the function, using MD5, javascript implementations of SHA-1 or SHA-256 algorithm.

* What do the algorithms do ?

Basicly it takes a string that you'll give it as parameter and after a bit of computation will return it under an hashed form.

Ex: "Salut à vous" (hello you) returns "807a56a1e5736f1b9fe533dfcb06ebfd"

The serie of numbers and letters is a hash. A MD5 hash in hexadecimal output.

Depending on the algorithm and the function, the returned hash will be different in length, use of characters

- It can be used to compare large amounts of data. You create the hashes for the data, store the hashes and later if you want to compare the data, you just compare the hashes. (known as MD5 checksum)

- Hashes can be used to index data. They can be used in hash tables to point to the correct row. If you want to quickly find a record, you calculate the hash of the data and directly go to the record where the corresponding hash record is pointing to. (This assumes that you have a sorted list of hashes that point to the actual records)

- They can be used in cryptographic applications like digital signatures. (That was my main purpose when developping the plugin)

- Hashing can be used to generate seemingly random strings. (Like in the CBhash Demo Game)

Here are the applications of hash functions that wikipedia lists:

Finding duplicate records

Finding similar records

Finding similar substrings

Geometric hashing

(source stackoverflow)

* Do I need those ?

It depends on you and your(s) project(s). Anyway, having the plugin in Construct 2's folder hierarchy is nothing more than having one more tool in the toolbox, ready to serve when you need it.

*Get Started

First you need to download CB Hash.zip in the forum topic (if you haven't already) and unzip its content into:

    Root_C2_Folder\exporters\html5\plugins

It should create the folder "CBhash".

You have the correct folder in the correct spot ?

All right then, launch Construct2.

This is how the icon of CB Hash looks in C2 object window. It is a beef. And some binary in the background. Yeah :)

Double-click this icon to include a CB Hash object to your project.

! Warning : CB Hash is a "single instance" object that won't appear on your layer. !

Now you're ready to use hashes.

+ Inline help

I've documented every function as clearly as I could. You can find informations at any point in the action and expression windows.

Add Action window:

Parameters for Action window:

Expression list:

Add event window:

Also here are the .capx from the CB Hash Demo Tech and CB Hash Demo Game.

CBhash Demo Tech.Capx[21]

CBhash Demo Game.Capx (See the topic, there is another plugin to download, all is explained).

Both .Capx are commented (as clearly and exhaustivly as I could).

+ So what's under the hood ?

The algorithms follow the same pattern.

You can obtain a straight hash by providing just the data to encode. You may obtain hexadecimal or base64 format output.

This is the simple method by the name of the algorithm.

You can obtain a sort of password by providing data to encode and a character string (ex:"123zreE519Fbfilmlzdpacujbn") whose characters only will appear in the final hash. (there won't be other characters in the hash than those).

This is the method Algorithmpass

You can obtain a HMAC type hash of the algorithm. You may obtain hexadecimal or base64 format output.

Both login and storing passwords require hashes that include both the password, and a random number (Key). We could simply hash the two strings concatenated. However, there is an alternative technique called HMAC, which uses two hash operations and some extra logic. This is specifically designed to be secure for such purposes, making it preferable to simple concatenation.

This is the method HMAC-Algorithm.

+ ACE table

    Condition/event
    The plugin offers one event: "On any hash generated", trigered any time a hash is made using either the actions or the expressions.

    - Actions
    Algorithm configuration
    Set inner algorithm hexadecimal output format - The case of the letters A-F in hexadecimal output - Lowercase (default), Uppercase
    Sets the inner algorithm hexadecimal output format - The character used to pad base-64 output to a multiple of 3 bytes - No Padding ("")(default), "="- for strict RFC compliance.
    Those were supposed to be implemented as properties (not allowed in C2 at the moment for [i]pf_singleglobal[/i] objects). 
    You shouldn't really touch it (unless you have troubles synching the algorithm with the one of the server side). 
    
    MD5 Actions
        
    MD5 - hash (String to encode, Output Format)
    MD5 - password generation (String to encode, Character String)
    HMAC-MD5 - keyed hashes (Key, String to encode, Output Format)
    HMAC-MD5 - password generation ( Key, String to encode, Character String)
        	
    SHA-1 Actions
        
    SHA-1 - hash (String to encode, Output Format)
    SHA-1 - password generation (String to encode, Character String)
    HMAC-SHA-1 - keyed hashes (Key, String to encode, Output Format)
    HMAC-SHA-1 - password generation ( Key, String to encode, Character String)
        	
    SHA-256 Actions
        
    SHA-256 - hash (String to encode, Output Format)
    SHA-256 - password generation (String to encode, Character String)
    HMAC-SHA-256 - keyed hashes (Key, String to encode, Output Format)
    HMAC-SHA-256 - password generation ( Key, String to encode, Character String)
        
        
    Expressions
    "Hashed"
    Get the last hash made. Command "get_lastResult", effect: Returns the last hash made.
        	
    "Hashing" MD5
    Hash MD5 (Hexa). Command "MD5("data")", effect: MD5(Hexa) - Encodes the data provided with MD5 algorithm.
    Hash MD5 (base64). Command "MD5B("data")", effect:MD5(base64) - Encodes the data provided with MD5 algorithm.
    HMAC-MD5 Hash (Hexa). Command "HMACMD5(key,data)", effect:(Hexa) Encodes a data string using a string key in order to make a HMAC-MD5 (Hash-based Message Authentication Code) hash.
    HMAC-MD5 Hash (base64). Command "HMACMD5B(key,data)", effect:(base64) Encodes a data string using a string key in order to make a HMAC-MD5 (Hash-based Message Authentication Code) hash.
        
    "Password Generation"	
    MD5 password generation. Command "MD5pass(data,character string)", effect: Generate a password from data using only the characters from charstring.
    HMAC-MD5 - password generation. Command "HMACMD5pass(key, data, character string)", effect: Generate an HMAC-MD5 (Hash-based Message Authentication Code) password using provided data, key and character string.
        
        
    "Hashing" SHA-1
    Hash SHA-1 (Hexa). Command "SHA1("data")", effect: SHA-1(Hexa) - Encodes the data provided with SHA-1 algorithm.
    Hash SHA-1 (base64). Command "SHA1B("data")", effect:SHA-1(base64) - Encodes the data provided with SHA-1 algorithm.
    HMAC-SHA-1 Hash (Hexa). Command "HMACSHA1(key,data)", effect:(Hexa) Encodes a data string using a string key in order to make a HMAC-SHA-1 (Hash-based Message Authentication Code) hash.
    HMAC-SHA-1 Hash (base64). Command "HMACSHA1B(key,data)", effect:(base64) Encodes a data string using a string key in order to make a HMAC-SHA-1 (Hash-based Message Authentication Code) hash.
        
    "Password Generation"	
    SHA-1 password generation. Command "SHA1pass(data,character string)", effect: Generate a password from data using only the characters from charstring.
    HMAC-SHA-1 - password generation. Command "HMACSHA1pass(key, data, character string)", effect: Generate an HMAC-SHA-1 (Hash-based Message Authentication Code) password using provided data, key and character string.
        
        
    "Hashing" SHA-256
    Hash SHA-256 (Hexa). Command "SHA256("data")", effect: SHA-256(Hexa) - Encodes the data provided with SHA-256 algorithm.
    Hash SHA-256 (base64). Command "SHA256B("data")", effect:SHA-256(base64) - Encodes the data provided with SHA-256 algorithm.
    HMAC-SHA-256 Hash (Hexa). Command "HMACSHA256(key,data)", effect:(Hexa) Encodes a data string using a string key in order to make a HMAC-SHA-256 (Hash-based Message Authentication Code) hash.
    HMAC-SHA-256 Hash (base64). Command "HMACSHA256B(key,data)", effect:(base64) Encodes a data string using a string key in order to make a HMAC-SHA-256 (Hash-based Message Authentication Code) hash.
        
    "Password Generation"	
    SHA-256 password generation. Command "SHA256pass(data,character string)", effect: Generate a password from data using only the characters from charstring.
    HMAC-SHA-256 - password generation. Command "HMACSHA256pass(key, data, character string)", effect: Generate an HMAC-SHA-256 (Hash-based Message Authentication Code) password using provided data, key and character string.
    
    

* The future/Extending the plugin

So as you can see, so far three algorithms coexist within the same plugin. And I believe we can put a lot more there.

The advantage of ectending with javascript, all you need is a notepad. No hard to set, time taking, software requiring compiling. You edit a fex text files, and here you have your plugin sitting in Construct2.

So this CornedBeef Hash could very well be extended by anyone of the community. And I urge you to do so. Please make it live, add it some other hashing useful algorithms.

I believe that JS allows such continuity. Let's try to keep a plugin per topic, but a very well featured plugin.

*I hope this tutorial helped you and that you will find some use for this hashes.

Thanks to Lucid for the name. Thanks to Zack0Wack0 for answering my first question in Javascript and looked upon the first implementation of this plugin.

And thanks to Ashley, Tom and every other person who ever worked on Construct Classic and Construct 2.*

CB Hash on the forum

  • 0 Comments

Want to leave a comment? Login or Register an account!