Silverlight – Hash SHA1 dans vos applications

by Fabien Lavocat 26. novembre 2009 16:40

Silverlight

Aujourd’hui, j’ai été confronté à un problème de sécurité dans une application Silverlight que je réalise (vous en verrez un peu plus prochainement). J’utilise de l’authentification pour des utilisateurs, or pour hacher des mots de passe, il n’y a pas de méthode managée toute prête qui prend en paramètre une chaine de caractère et qui ressort le hash SHA1. Nous allons donc créer une méthode d’extention. Commencer par importer les espaces de noms suivants :

using System.Security.Cryptography;
using System.Text;

Ensuite, dans une classe statique, nous créons la méthode suivante :

public static String ToSHA1(this String entry)
{
   
SHA1Managed sHA1Managed = new SHA1Managed();
   
Byte[] result = sHA1Managed.ComputeHash(new UTF8Encoding().GetBytes(entry));
   
StringBuilder hashedString = new StringBuilder();
   
foreach (Byte outputByte in result)
        hashedString.Append(outputByte.ToString(
"x2").ToUpper());

   
return hashedString.ToString();
}

Nous pouvons donc maintenant l’utiliser de cette façon :

String password = "password";
String hashedPassword = password.ToSHA1();

Tags:

Silverlight

Commentaires

03/12/2009 17:51:46 #

Nk54

Et hop ! Dans mes favoris rubrique Silverlight/Sécurité Smile

Merci à toi.

Nk54 France

05/12/2009 17:45:02 #

fabien lavocat

Merci Smile

fabien lavocat France

Ajouter un commentaire


(Affichera votre icône Gravatar)

  Country flag

biuquote
  • Commentaire
  • Aperçu immédiat
Loading



A propos de l'auteur

Fabien Lavocat

Lavocat Fabien
Ingénieur Multimédia - TMM Communication



"Blog-Microsoft.fr is an independent blog and is not affiliated with, nor has it been authorized, sponsored, or otherwise approved by Microsoft Corporation."

Contactez-moi Send mail

MVP
Microsoft Most Valuable Professional
Client Application Development