Advertisement
onzulin

objeto CryptoPass.cs

Oct 21st, 2013
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Security.Cryptography;
  7. using PlanningActivitiesWP8.Entidades;
  8. using Microsoft.WindowsAzure.MobileServices;
  9.  
  10. namespace PlanningActivitiesWP8
  11. {
  12.     public class CryptoPass
  13.     {
  14.         #region propiedades o autopropiedades
  15.        
  16.         #endregion
  17.  
  18.         #region constructor
  19.         public CryptoPass()
  20.         {
  21.         }
  22.         #endregion
  23.         #region metodos para cifrar datos
  24.         /* GenerateHash es para generar el hash de la clave y necesito el codigo salt
  25.          */
  26.        
  27.         public static byte[] GenerateHash(string password, byte[] salt)
  28.         {
  29.             byte[] passwordData = Encoding.UTF8.GetBytes(password);
  30.  
  31.             byte[] composite = new byte[passwordData.Length + 32];
  32.  
  33.             Array.Copy(passwordData, composite, passwordData.Length);
  34.             Array.Copy(salt, 0, composite, passwordData.Length, salt.Length);
  35.  
  36.             SHA256 hashFunction = new SHA256Managed();
  37.             byte[] hash = hashFunction.ComputeHash(composite);
  38.  
  39.             return hash;
  40.         }
  41.        
  42.         // obtener salt como dice el nombre del metodo que es lo primero que hay que hacer
  43.        
  44.         public static byte[] GetSalt()
  45.         {
  46.             byte[] rngContainer = new byte[32];
  47.             RNGCryptoServiceProvider rngProvider = new RNGCryptoServiceProvider();
  48.             rngProvider.GetBytes(rngContainer);
  49.  
  50.             return rngContainer;
  51.         }
  52.  
  53.         public static byte[] GetSaltFromString(string source)
  54.         {
  55.             string[] raw = source.Split('-');
  56.             byte[] result = new byte[raw.Length];
  57.             for (int i = 0; i < raw.Length; i++)
  58.             {
  59.                 result[i] = Convert.ToByte(raw[i], 16);
  60.             }
  61.             return result;
  62.         }
  63.  
  64.         /* aqui tengo que hacer modificacions en el codigo del metodo para que funcione como es debido
  65.          */
  66.         //aqui definimos el user que vamos a añadir luego con el comando RegisterUser
  67.         public static usuarios GetSecureUserModel(string username, string password, string email = "", byte[] customSalt = null)
  68.         {
  69.             byte[] hash;
  70.  
  71.             usuarios user = new usuarios();
  72.             user.usuario = username;
  73.             user.email = email;
  74.  
  75.             if (customSalt == null)
  76.             {
  77.                 byte[] salt = GetSalt();
  78.                 user.salt = BitConverter.ToString(salt);
  79.  
  80.                 hash = GenerateHash(password, salt);
  81.  
  82.             }
  83.             else
  84.             {
  85.                 hash = GenerateHash(password, customSalt);
  86.             }
  87.  
  88.             user.hash = BitConverter.ToString(hash);
  89.             user.salt = BitConverter.ToString(customSalt);
  90.             return user;
  91.         }
  92.        
  93.         public async static Task<bool> RegisterUser(usuarios user)
  94.         {
  95.             IMobileServiceTable<usuarios> userTable = App.MobileService.GetTable<usuarios>();
  96.             List<usuarios> userList = await userTable.Take(1).Where(x => x.usuario == user.usuario).ToListAsync();
  97.            
  98.             if (userList.Count == 0)
  99.             {
  100.                 await App.MobileService.GetTable<usuarios>().InsertAsync(user);
  101.                 return true;
  102.             }
  103.             else
  104.             {
  105.                 return false;
  106.             }
  107.         }
  108.         //obten4er usuario de la base de datos
  109.         public static async Task<usuarios> GetUserFromDatabase(string username)
  110.         {
  111.             IMobileServiceTable<usuarios> userTable = App.MobileService.GetTable<usuarios>();
  112.             List<usuarios> userList =  await userTable.Take(1).Where(x => x.usuario == username).ToListAsync();
  113.  
  114.             if (userList.Count == 0)
  115.                 return null;
  116.             else
  117.                 return userList.First();
  118.         }
  119.        
  120.         public async static Task<usuarios> VerifyLogin(string username, string password)
  121.         {
  122.             usuarios dbUser = await  GetUserFromDatabase(username);
  123.  
  124.             if (dbUser != null)
  125.             {
  126.                 usuarios localUser = GetSecureUserModel(username, password, "",
  127.                     GetSaltFromString(dbUser.salt));
  128.                 if (dbUser.hash == localUser.hash)
  129.                     return dbUser;
  130.             }
  131.  
  132.             return null;
  133.         }
  134.  
  135.  
  136.         #endregion
  137.  
  138.  
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement