2016-08-11 1 views
-1

Je application à écrire pour UWPSHA1Managed UWP (C#)

J'ai le code

private string Hash(string input) 
{ 
    using (SHA1Managed sha1 = new SHA1Managed()) 
    { 
     var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(input)); 
     var sb = new StringBuilder(hash.Length * 2); 

     foreach (byte b in hash) 
     { 
      // can be "x2" if you want lowercase 
      sb.Append(b.ToString("X2")); 
     } 

     return sb.ToString(); 
    } 
} 

mais elle ne fonctionne et montrer cette erreur

Code de gravité Description du projet État Suppression de la ligne de fichier Erreur CS0246 Le nom de type ou d'espace de nom 'SHA1Managed' est introuvable (une directive using ou une référence d'assembly est-elle manquante?) Milano C: \ Utilisateurs \ nemes \ Documents \ GitHub \ Milano_pizza \ Milano \ WoocommerceApiCli ent.cs 25 Actif

Comment puis-je résoudre ce problème?

+0

Le code que vous avez écrit était pour .NET Framework. UWP ne supporte pas le type et vous devez passer aux classes de cryptographie UWP. –

Répondre

3

Pour UWP utilisation HashAlgorithmProvider

public string Hash(string input) 
    { 
     IBuffer buffer = CryptographicBuffer.ConvertStringToBinary(input, BinaryStringEncoding.Utf8); 
     HashAlgorithmProvider hashAlgorithm = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1); 
     var hashByte = hashAlgorithm.HashData(buffer).ToArray(); 
     var sb = new StringBuilder(hashByte.Length * 2); 
     foreach (byte b in hashByte) 
     { 
      sb.Append(b.ToString("x2")); 
     } 
     return sb.ToString(); 
    } 

Rappelez-vous d'ajouter

using Windows.Security.Cryptography; 
using Windows.Security.Cryptography.Core;