class TruTrace
{

	/// <summary>
	///  {[9F06]#[50]#[last 4 digits 5A]#[5F25]#[5F28]#[5F2D]#[5F34]#[SHA256(S1 + [5F20])]#[SHA256(S1 + [5F24])]}
	/// </summary>
	/// <returns></returns>
	public override string ToString()
	{
		SHA256 sha256 = SHA256.Create();

		string IIN_42 = Check(IssuerIdentificationNumber);
		string AID_9F06 = Check(ApplicationIdentifier);
		string AL_50 = Check(ApplicationLabel);
		string L4PAN_5A = Check(Last4DigitsPAN, 4);
		string AED_5F25 = Check(ApplicationEffectiveDate);
		string ICC_5F28 = Check(IssuerCountryCode);
		string LP_5F2D = Check(LanguagePreference);
		string PANSeq_5F34 = Check(PANSequenceNumber);
		string CN_5F20 = Check(CardholderName);
		string AExD_5F24 = Check(ApplicationExpirationDate);

		string s1 = IIN_42 + L4PAN_5A;
		string scrambledHashedCardholderName = generateHash(s1 + CN_5F20);
		string scrambledHashedExpiryDate = generateHash(s1 + AExD_5F24);


		string rv =
		string.Format("{0}#{1}#{2}#{3}#{4}#{5}#{6}#{7}#{8}",
			AID_9F06,
			AL_50,
			L4PAN_5A,
			AED_5F25,
			ICC_5F28,
			LP_5F2D,
			PANSeq_5F34,
			scrambledHashedCardholderName,
			scrambledHashedExpiryDate
			);

		return rv;
	}

	/// <summary>
	///  42	 Issuer Identification Number(IIN)
	/// </summary>
	public String IssuerIdentificationNumber { get; set; } = String.Empty;

	/// <summary>
	/// 9F06	Application Identifier(AID) - Terminal
	/// </summary>
	public String ApplicationIdentifier { get; set; } = String.Empty;

	/// <summary>
	/// 50	Application Label
	/// </summary>
	public String ApplicationLabel { get; set; } = String.Empty;

	/// <summary>
	/// ****5A Last 4 digits(PAN)
	/// </summary>
	public String Last4DigitsPAN { get; set; } = String.Empty;

	/// <summary>
	///  5F25	Application Effective Date
	/// </summary>
	public String ApplicationEffectiveDate { get; set; } = String.Empty;

	/// <summary>
	/// 5F28	Issuer Country Code
	/// </summary>
	public String IssuerCountryCode { get; set; } = String.Empty;

	/// <summary>
	/// 5F2D	Language Preference
	/// </summary>
	public String LanguagePreference { get; set; } = String.Empty;

	/// <summary>
	/// 5F34	Application Primary Account Number Sequence Number
	/// </summary>
	public String PANSequenceNumber { get; set; } = String.Empty;

	/// <summary>
	/// 5F20	Cardholder Name
	/// </summary>
	public String CardholderName { get; set; } = String.Empty;

	/// <summary>
	///  5F24	Application Expiration Date
	/// </summary>
	public String ApplicationExpirationDate { get; set; } = String.Empty;

	string generateHash(string data)
	{
		byte[] h1 = SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(data));
		return BitConverter.ToString(h1).Replace("-", string.Empty);
	}

	string Check(String value)
	{
		return (value == null) ? String.Empty : value;
	}
	string Check(String value, int len)
	{
		string rv = (value == null) ? String.Empty : value;
		if ((rv.Length > 0) && (rv.Length != len) && (len > 0) && (len < (rv.Length + 16)))
		{
			rv = ("000000" + rv);  
			rv = rv.Substring(rv.Length - len);
		}
		return rv;
	}
}
Feedback