public byte[] ToByteArray() { using (var ms = new MemoryStream()) { using (var writer = new BinaryWriter(ms)) { // Write the aaguid bytes out, reverse if we're on a little endian system if (BitConverter.IsLittleEndian) { writer.Write(AaGuidToBigEndian(AaGuid)); } else { writer.Write(AaGuid.ToByteArray()); } // Write the length of credential ID, as big endian bytes of a 16-bit unsigned integer var credentialIDLen = (ushort)CredentialID.Length; var credentialIDLenBytes = BitConverter.GetBytes(credentialIDLen); if (BitConverter.IsLittleEndian) { Array.Reverse(credentialIDLenBytes); } writer.Write(credentialIDLenBytes); // Write CredentialID bytes writer.Write(CredentialID); // Write credential public key bytes writer.Write(CredentialPublicKey.GetBytes()); } return(ms.ToArray()); } }
/// <summary> /// AAGUID is sent as big endian byte array, this converter is for little endian systems. /// </summary> private byte[] AaGuidToBigEndian() { var aaguid = AaGuid.ToByteArray(); SwapBytes(aaguid, 0, 3); SwapBytes(aaguid, 1, 2); SwapBytes(aaguid, 4, 5); SwapBytes(aaguid, 6, 7); return(aaguid); }