public static void Main()
Console.WriteLine(GetPanForUDKDerive("4427808001112223337","01"));
public static string GetPanForUDKDerive(string pan, string panSeqNo)
if (pan.IndexOf('F') > 0)
pan = pan.Substring(0, pan.IndexOf('F'));
temp = pan.PadLeft(16, '0') + panSeqNo.PadRight(2, '0');
temp = temp.Substring(temp.Length - 16, 16);
temp = pan + panSeqNo.PadRight(2, '0');
byte[] HashCode = new byte[40];
HashCode = ComputeHashStr(temp).Digest;
string HashStr = UnPack(HashCode);
for (byte i = 0; i < 40 && temp.Length < 16; i++)
if (HashStr[i] >= '0' && HashStr[i] <= '9')
for (byte i = 0; i < 40 && temp.Length < 17; i++)
if (HashStr[i] >= 'A' && HashStr[i] <= 'F')
temp += (Convert.ToByte(HashStr[i]) - 65).ToString();
temp += XOR("FFFFFFFFFFFFFFFF", temp);
public static HashResponse ComputeHashStr(string buffer)
var res = new HashResponse();
using (var sha1 = System.Security.Cryptography.SHA1.Create())
res.Digest = sha1.ComputeHash(Pack(buffer));
res.Length = res.Digest.Length;
public class HashResponse
public int Length { get; set; }
public byte[] Digest { get; set; }
public static string XOR(string s1, string s2)
if (s1.Length != s2.Length)
throw new ArgumentException("ByteArray.XOR parameter1 and parameter2 must have equal length");
if ((s1.Length % 2) != 0)
throw new ArgumentException("ByteArray.XOR invalid length of parameters");
byte[] ba = new byte[ba1.Length];
for (int i = 0; i < ba1.Length; i++)
ba[i] = (byte)(ba1[i] ^ ba2[i]);
public static byte[] Pack(string hexData)
if (IsHex(hexData) == false)
throw new FormatException("Non hex digit found");
if (hexData.Length % 2 != 0)
throw new ArgumentException("Data length is odd");
byte[] binaryData = new byte[hexData.Length / 2];
for (int i = 0; i < binaryData.Length; i++)
hex = new String(new Char[] { hexData[j], hexData[j + 1] });
binaryData[i] = byte.Parse(hex, System.Globalization.NumberStyles.HexNumber);
public static string UnPack(byte[] binaryData)
StringBuilder hexData = new StringBuilder();
foreach (byte binary in binaryData)
hexData.Append(binary.ToString("X2"));
return hexData.ToString();
public static bool IsHex(string data)
return System.Text.RegularExpressions.Regex.IsMatch(data, "^[0-9a-fA-F]*$");