tbf/GemCard/APDUResponse.cs

75 lines
1.9 KiB
C#
Raw Normal View History

using System;
using System.Text;
namespace GemCard
{
/// <summary>
/// This class represents the APDU response sent by the card
/// </summary>
public class APDUResponse
{
/// <summary>
/// Status bytes length
/// </summary>
public const int SW_LENGTH = 2;
/// <summary>
/// Response data. Contains the data sent by the card minus the 2 status bytes (SW1, SW2)
/// null if no data were sent by the card
/// </summary>
public byte[] Data;
public byte SW1;
public byte SW2;
/// <summary>
/// Status get property
/// </summary>
public ushort Status
{
get { return (ushort)(((short)SW1 << 8) + (short)SW2); }
}
/// <summary>
/// Constructor from the byte data sent back by the card
/// </summary>
/// <param name="baData">Buffer of data from the card</param>
public APDUResponse(byte[] baData)
{
if (baData.Length > SW_LENGTH)
{
Data = new byte[baData.Length - SW_LENGTH];
for (int nI = 0; nI < baData.Length - SW_LENGTH; nI++)
{
this.Data[nI] = baData[nI];
}
}
this.SW1 = baData[baData.Length - 2];
this.SW2 = baData[baData.Length - 1];
}
/// <summary>
/// Overrides the ToString method to format to a string the APDUResponse object
/// </summary>
/// <returns></returns>
public override string ToString()
{
if (Data != null)
{
StringBuilder sData = new StringBuilder(Data.Length * 2);
for (int nI = 0; nI < Data.Length; nI++)
{
sData.AppendFormat("{0:X02}", Data[nI]);
}
return sData.ToString();
}
else
{
return string.Empty;
}
}
}
}