How is a ViridiCode hash calculated?
  • 24 Jun 2024
  • 2 Minutes to read
  • Contributors
  • Dark
    Light
  • PDF

How is a ViridiCode hash calculated?

  • Dark
    Light
  • PDF

Article summary

<span class="fr-marker" data-id="0" data-type="true" style="display: none; line-height: 0;"></span><span class="fr-marker" data-id="0" data-type="false" style="display: none; line-height: 0;"></span<span class="fr-marker" data-id="0" data-type="true" style="display: none; line-height: 0;"></span><span class="fr-marker" data-id="0" data-type="false" style="display: none; line-height: 0;"></span>The ViridiCode barcode is quite long and therefore cannot be mentioned as complete readable text under a  barcode, something that is normal for standard barcodes such as Ean8 and Ean13. However, it is necessary to recognize the barcode, for example, to choose the correct one from a list of barcodes to put an offer on it.

To make this possible, NedFox has developed an algorithm that creates a 'hash' of a ViridiCode. This is best compared to a check digit, but the ViridiCode hash contains not only numbers but also letters. The hash code is a 'one way' encryption; it is not possible to reconstruct the original ViridiCode from a hash code.

The hash code is not guaranteed to be unique, but that is not necessary. The recognizability of a certain ViridiCode is what matters. The chance that someone will ever find the same hash on different ViridiCodes that are on the market at the same time is extremely small.

It is desired to print the hash code on a ViridiCode label, below or next to the ViridiCode (datamatrix barcode) itself. The hash is calculated based on the .Net code below and consists of an MD5 hash that is then Base64 encoded. The code below uses an example ViridiCode, where the result should be NQLZJHJFAQ. When transferring this code to, for example, PHP, it is essential that the outcome is identical, otherwise invalid/non-existent hash codes will be displayed in the store.

After all, RetailVista also calculates these hash codes itself and they must therefore match the labels.

Support for this code is only provided in our online forum, which can be found at https://forum.nedfox.net

using NedFox.Sdk;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Security.Cryptography;

using System.Text;

using System.Threading.Tasks;

namespace ConsoleApp1

{

    class Program

    {

        static void Main(string[] args) {

            string viridiCode = "0108719613726796~988718288066800~800920191028144744489~240VBNL41893~427ORNR274614~992EUR2499";

            // Expected hash result: NQLZJHJFAQ

            // Replace special ViridiCode / 2D barcode separator chars like char 15, 29, ~ and | symbols for 'empty separators'

            // Base64 contains 0-9 and A-Z and a-z and + and / chars as possible return values. Replace + en / chars for 9, we want A-Z and 0-9 only!

            string base64Result = Encrypt(viridiCode.ToUpper().Replace((char)15, ' ').Replace((char)29, ' ').Replace('~', ' ').Replace('|', ' ').Replace(" ", ""));

            // Strip typical Base64 results like ==, + and / chars

            string finalHash = base64Result.Replace("==", "").Substring(0, 10).ToUpper().Replace('+', '9').Replace('/', '9');

            Console.WriteLine(finalHash);

            Console.Translate the following text from nl-NL to en-US and use product instead of article and keep the html content: ReadLine();

        }

        private static string Encrypt(string text) {

            string salt = "NedFox";

            string preSalt = salt.Substring(0, salt.Length / 2);

            string postSalt = salt.Substring(salt.Length / 2);

            MD5CryptoServiceProvider cryptoProvider = new MD5CryptoServiceProvider();

            string toEncrypt = preSalt + text + postSalt;

            byte[] encrypted = cryptoProvider.ComputeHash(Encoding.UTF32.GetBytes(toEncrypt));

            StringBuilder sb = new StringBuilder(encrypted.Length);

            sb.Append(System.Convert.ToBase64String(encrypted));

            return sb.ToString();

        }

    }

}


Was this article helpful?

Changing your password will log you out immediately. Use the new password to log back in.
First name must have atleast 2 characters. Numbers and special characters are not allowed.
Last name must have atleast 1 characters. Numbers and special characters are not allowed.
Enter a valid email
Enter a valid password
Your profile has been successfully updated.