Advertisement
Guest User

Untitled

a guest
Jul 8th, 2021
869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Claims;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using Jose;
  9. using Newtonsoft.Json;
  10.  
  11. namespace Visa
  12. {
  13.     public static class EncryptionHelper
  14.     {
  15.         /// <summary>
  16.         /// https://github.com/jwt-dotnet/jwt  
  17.         /// </summary>
  18.         /// <param name="jsonData">Raw data for encryption</param>
  19.         /// <param name="sharedSecret">EncryptionSharedSecret VISA</param>
  20.         /// <param name="apiKey">EncryptionApiKey VISA</param>
  21.         /// <returns></returns>
  22.         public static string JwtTokenEncryption(string jsonData, string sharedSecret, string apiKey)
  23.         {
  24.             using (SHA256 sha256Hash = SHA256.Create())
  25.             {
  26.                 var utc0 = new DateTime(1970, 1, 1, 3, 0, 0, 0, DateTimeKind.Utc);
  27.                 var iat = (int)DateTime.Now.Subtract(utc0).TotalSeconds;
  28.  
  29.                 var digest = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(sharedSecret));
  30.  
  31.                 var extraHeaders = new Dictionary<string, object>
  32.                 {
  33.                     {"kid",  apiKey},
  34.                     {"typ", "JOSE"},
  35.                     {"channelSecurityContext", "SHARED_SECRET"},
  36.                     {"iat", iat},
  37.                 };
  38.  
  39.                 string result = Jose.JWT.Encode(jsonData, digest, JweAlgorithm.A256GCMKW, JweEncryption.A256GCM, extraHeaders: extraHeaders);
  40.  
  41.                 return result;
  42.             }
  43.         }
  44.  
  45.         /// <summary>
  46.         /// https://github.com/jwt-dotnet/jwt  
  47.         /// </summary>
  48.         /// <param name="token">Token from VISA</param>
  49.         /// <param name="sharedSecret">EncryptionSharedSecret VISA</param>
  50.         /// <returns></returns>
  51.         public static string JwtTokenDecryption(string token, string sharedSecret)
  52.         {
  53.             using (SHA256 sha256Hash = SHA256.Create())
  54.             {
  55.                 var digest = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(sharedSecret));
  56.                
  57.                 string result = Jose.JWT.Decode(token, digest, JweAlgorithm.A256GCMKW, JweEncryption.A256GCM);
  58.  
  59.                 return result;
  60.             }
  61.         }
  62.  
  63.         public static string getXPayToken(string secret, string resourcePath, string queryString, string requestBody = "")
  64.         {
  65.             string timestamp = getTimestamp();
  66.             string sourceString = timestamp + resourcePath + queryString + requestBody;
  67.             string hash = getHash(sourceString, secret);
  68.             string token = "xv2:" + timestamp + ":" + hash;
  69.  
  70.             return token;
  71.         }
  72.  
  73.         private static string getTimestamp()
  74.         {
  75.             long timeStamp = ((long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds) / 1000;
  76.  
  77.             return timeStamp.ToString();
  78.         }
  79.  
  80.         private static string getHash(string data, string secret)
  81.         {
  82.             var hashString = new HMACSHA256(Encoding.ASCII.GetBytes(secret));
  83.             var hashBytes = hashString.ComputeHash(Encoding.ASCII.GetBytes(data));
  84.             string digest = String.Empty;
  85.  
  86.             foreach (byte b in hashBytes)
  87.             {
  88.                 digest += b.ToString("x2");
  89.             }
  90.  
  91.             return digest;
  92.         }
  93.     }
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement