locked
Is there any RSA, SHA API in WinRT javascript?

    Question

  • Hi,

    Is there any RSA or SHA API in WinRT javascript? 

     

    Thanks,

     

     


    Louis
    • Edited by Louis_PiG Thursday, February 02, 2012 11:45 PM
    Thursday, February 02, 2012 11:29 PM

Answers

All replies

  • The WinRT API provides SHA functionality in the Windows.Security.Cryptography.Core namespace, specifically through the static method HashAlgorithmProvider.openAlgorithm(Windows.Security.Cryptography.Certificates.HashAlgorithmNames.sha256).

     

    This provides you with a HashAlgorithmProvider class that has methods like hashData. There is an example of using this on http://msdn.microsoft.com/en-us/library/windows/apps/hh464979(v=vs.85).aspx.

     

    The topic http://msdn.microsoft.com/en-us/library/windows/apps/hh464913(v=vs.85).aspx also has some more general info about such algorithms.

    .Kraig

    • Marked as answer by Louis_PiG Friday, February 03, 2012 7:47 AM
    Friday, February 03, 2012 3:01 AM
  • Kraig your first link is broken now - can you find the new version?
    Sunday, September 30, 2012 3:42 AM
  • You could do it like this for strings:

    String.prototype.base64 = function() {
        return CryptographicBuffer.encodeToBase64String(CryptographicBuffer.convertStringToBinary(this, BinaryStringEncoding.utf8));
      };
    
      String.prototype.hash = function(method) {
        var hashData, provider;
        provider = HashAlgorithmProvider.openAlgorithm(method.toUpperCase());
        hashData = provider.hashData(CryptographicBuffer.convertStringToBinary(this, BinaryStringEncoding.utf8));
        if (hashData.length !== provider.hashLength) {
          throw new WinJS.ErrorFromName('InvalidArgumentException', "Generated hash length is different that algorithm hash length");
        }
        return CryptographicBuffer.encodeToHexString(hashData);
      };
    
      String.prototype.sha1 = function() {
        return this.hash("SHA1");
      };
    
      String.prototype.md5 = function() {
        return this.hash("MD5");
      };
    
      String.fromBase64 = function(base64) {
        return CryptographicBuffer.convertBinaryToString(BinaryStringEncoding.utf8, CryptographicBuffer.decodeFromBase64String(base64));
      };
    

    Could be easily extended to calc hashes from IBuffer or arrays.

    Monday, October 01, 2012 12:02 PM