Generate MAC address hash and save to database

Asked

Viewed 158 times

0

According to my question to get the address mac of the network card and generate its hash, the following methods are used:

private string GetSHA1HashData(string data)
    {
        SHA1 sha1 = SHA1.Create();

        byte[] hashData = sha1.ComputeHash(Encoding.Default.GetBytes(data));

        StringBuilder returnValue = new StringBuilder();

        for (int i = 0; i < hashData.Length; i++)
        {
            returnValue.Append(hashData[i].ToString());
        }

        return returnValue.ToString();
    }

    private string GetMacAddress()
    {
        string macAddresses = string.Empty;

        foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
        {
            if (nic.OperationalStatus == OperationalStatus.Up)
            {
                macAddresses += nic.GetPhysicalAddress().ToString();
                break;
            }
        }

        return macAddresses;
    }

But how do I save this hash in the database ? Remembering here that I already have the model done and the table already created:

public class Chave
{
    [Key]
    public string EnderecoMacRede { get; set; }
}
  • Will you use a separate table to save the address? At what point of system use should you take the data and insert it?

  • Exact. So the application already startar the first time already keeps the hash. And the subsequent times will only make the check.

1 answer

1


It’s important that you don’t make this information easy for someone who’s trying to break your system, so this denomination is not a good one.

Change to the following:

public class Licenca
{
    [Key]
    public string Chave { get; set; }
}

The right way is to use a Migration to generate key information for you. Something like this:

namespace SeuProjeto.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    public sealed class Configuration : DbMigrationsConfiguration<SeuProjeto.Models.SeuProjetoContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
            AutomaticMigrationDataLossAllowed = true;
        }

        protected override void Seed(SeuProjeto.Models.SeuProjetoContext context)
        {
            // Estou supondo que você implementou os métodos dentro de uma classe
            // estática, então ficaria:
            var chave = LicencasHelper.GerarLicenca();

            context.Licencas.AddOrUpdate(
                l => l.Chave,
                new Licenca
                {
                    Chave = chave
                });

            context.SaveChanges();
        }
    }
}

The class LicencasHelper would be something like:

public static class LicencasHelper
{
    public static string GerarLicenca() 
    {
        return GetSHA1HashData(GetMacAddress());
    }

    private static string GetSHA1HashData(string data)
    {
        SHA1 sha1 = SHA1.Create();

        byte[] hashData = sha1.ComputeHash(Encoding.Default.GetBytes(data));

        StringBuilder returnValue = new StringBuilder();

        for (int i = 0; i < hashData.Length; i++)
        {
            returnValue.Append(hashData[i].ToString());
        }

        return returnValue.ToString();
    }

    private static string GetMacAddress()
    {
        string macAddresses = string.Empty;

        foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
        {
            if (nic.OperationalStatus == OperationalStatus.Up)
            {
                macAddresses += nic.GetPhysicalAddress().ToString();
                break;
            }
        }

        return macAddresses;
    }
}

In subsequent publications, consider commenting on the code of Migration in order to prevent reverse engineering.

Another thing that can be done is to separate the code from the Helper in a DLL just for it, and remove the reference when the Migration is performed, but this is only if you do not need to check the license on login.

In any case, a Action login would look like this:

public ActionResult Login() 
{
    var licenca = context.Licencas.FirstOrDefault();

    if (licenca == null) return View("SemLicenca");

    if (licenca.Chave != LicencasHelper.GerarLicenca()) return View("LicencaInvalida");

    return View();
}
  • Now it’s much clearer Gypsy. But, how can I save this hash once only, and once application start and how can I do this check on my controller and not let go of the login page for example ? Since the hash will not be present.

  • Well, Migration guarantees this unique save. As you will always check the hash then LicencasHelper cannot leave the code. In Action Login, you bring Hash from the bank, call LicencasHelper again and compares the two hashes. If they are the same, you let them log in. Otherwise, it displays a View saying that the license does not agree.

  • Could you put that suggestion in the answer too ?

  • @Érikthiago Feito.

  • Very good ! I will implement here and warn you !

  • Funcionoooooou !!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.