Advertisement
uraharadono

PW Hash and Salt, Image Crop

Jan 19th, 2016
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.51 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Linq;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace frozenDeal_UI.Util
  11. {
  12.     public class UIHelper
  13.     {
  14.         #region Korisnici
  15.         public static string GenerateSalt()
  16.         {
  17.             byte []arr=new byte[16];
  18.             RNGCryptoServiceProvider cripto = new RNGCryptoServiceProvider();
  19.             cripto.GetBytes(arr);
  20.             return Convert.ToBase64String(arr);
  21.         }
  22.  
  23.         public static string GenerateHash(string lozinka, string salt)
  24.         {
  25.             byte[] pass = Encoding.Unicode.GetBytes(lozinka);
  26.             byte[] dodatak = Convert.FromBase64String(salt);
  27.             byte[] forHash = new byte[pass.Length + dodatak.Length];
  28.  
  29.             System.Buffer.BlockCopy(pass, 0, forHash, 0, pass.Length);
  30.             System.Buffer.BlockCopy(dodatak, 0, forHash, pass.Length,dodatak.Length);
  31.  
  32.             HashAlgorithm alg = HashAlgorithm.Create("SHA1");
  33.             byte[] hashed=alg.ComputeHash(forHash);
  34.  
  35.             return Convert.ToBase64String(hashed);
  36.         }
  37.         #endregion
  38.  
  39.         #region Slike
  40.  
  41.         public static Image CropImage(Image img, Rectangle cropArea)
  42.         {
  43.             Bitmap bmpImage = new Bitmap(img);
  44.             Bitmap bmpCrop = bmpImage.Clone(cropArea,
  45.             bmpImage.PixelFormat);
  46.             return (Image)(bmpCrop);
  47.         }
  48.  
  49.         public static Image ResizeImage(Image imgToResize, Size size)
  50.         {
  51.             int sourceWidth = imgToResize.Width;
  52.             int sourceHeight = imgToResize.Height;
  53.  
  54.             float nPercent = 0;
  55.             float nPercentW = 0;
  56.             float nPercentH = 0;
  57.  
  58.             nPercentW = ((float)size.Width / (float)sourceWidth);
  59.             nPercentH = ((float)size.Height / (float)sourceHeight);
  60.  
  61.             if (nPercentH < nPercentW)
  62.                 nPercent = nPercentH;
  63.             else
  64.                 nPercent = nPercentW;
  65.  
  66.             int destWidth = (int)(sourceWidth * nPercent);
  67.             int destHeight = (int)(sourceHeight * nPercent);
  68.  
  69.             Bitmap b = new Bitmap(destWidth, destHeight);
  70.             Graphics g = Graphics.FromImage((Image)b);
  71.             g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  72.  
  73.             g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
  74.             g.Dispose();
  75.  
  76.             return (Image)b;
  77.         }
  78.  
  79.         #endregion
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement