Generate MAC address hash and save to database

0

According to this my question to get the mac address of the network card and generate the hash, use the following methods:

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 that I already have the model done and the table already created:

public class Chave
{
    [Key]
    public string EnderecoMacRede { get; set; }
}
    
asked by anonymous 26.11.2014 / 00:03

1 answer

1

It's important that you do not leave this information easy to someone trying to break your system, so this is not a good name.

Change to the following:

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

The correct 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 look 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 releases, consider commenting on the Migration code to prevent it from being reverse-engineered.

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

In any case,% 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();
}
    
26.11.2014 / 03:35