Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do MD5 with foreign characters?

Tags:

People also ask

Can MD5 have special characters?

In PowerCenter, the value of the MD5 Function with special characters in the SQL Server DB differs from the MD5 calculation of Informatica when Integration Service runs in the Unicode pass special characters.

Can hash have special characters?

The hash output is a random string of a length specified by the function (that is 160 bits for SHA-1). It may contain any special characters, including e.g. white space.

How long is an MD5 hash in characters?

Yup it's always 32 characters long. That's the way the MD5 hashing algorithm works.

What is MD5 () function?

What is MD5? MD5 (message-digest algorithm) is a cryptographic protocol used for authenticating messages as well as content verification and digital signatures. MD5 is based on a hash function that verifies that a file you sent matches the file received by the person you sent it to.


So I am making my custom highscore board in mobile game, using unity engine.

I setup my mysql database, and bought highscore asset from store, it works, but only with english user name. So basically it send user's name, score to .php script.

But I want that script also can receive korean characters as user's nickname. My users will use korean characters too as nickname not only english characters.

How can I achieve this?

Here are codes.

------------------(Highscore.cs at unity side)

WWWForm rsFm = new WWWForm();
            rsFm.AddField("name",name);   
        // at here name field, I want to receive korean characters as well.
            rsFm.AddField("tables",tables);
            rsFm.AddField("hash",GetHash(name));
            WWW rs = new WWW(registerUserURL,rsFm);
            yield return rs;

..................

string GetHash(string usedString){ //Create a Hash to send to server
    MD5 md5 = MD5.Create();
    byte[] bytes = Encoding.ASCII.GetBytes(usedString+secretKey);
    byte[] hash = md5.ComputeHash(bytes);

    StringBuilder sb = new StringBuilder();
    for(int i = 0; i < hash.Length; i++){
        sb.Append(hash[i].ToString("x2"));
    }
    return sb.ToString();

}

RegisterUser.php

<?php
include('ServerConnect.php');
$connection = Connect();//Attempt to Connect to MYSQL Server & DataBase

//Get variables from unity
$name = $_POST['name'];
$date = strtotime(date("Y-m-d"));
$tables = $_POST['tables'];
//Security Check
$hash = $_POST['hash'];
if(CheckKey($hash,$name) == 'fail'){ //Check if hash is valid
    echo 'Security Failure'; exit;

}

ServerConnect.php

function CheckKey($hash,$name){  //Check weather Md5 hash matches
    global $secretKey; 
    $checkHash = md5($name.$secretKey);
    if(strcmp($checkHash,$hash) == 0){
        return 'pass'; //hash matches
    }else{
        return 'fail'; //hash failed
    }

}

When I input korean character and send, console Result says "Security Failure" at above code.