Ejemplo n.º 1
0
        /// <summary>
        /// Decodes the specified QR string.
        /// </summary>
        /// <param name="qrstring">The QR string.</param>
        /// <returns>The payload data.</returns>
        /// <exception cref="InvalidDataException">Checksum of decompressed data does not match.</exception>
        public byte[] DecodeBytes(string qrstring)
        {
            // decode from Base32Hex
            var data2 = Base32.FromBase32String(qrstring);

            // data[0-1] // PayBySquare header
            // data[2-3] // original data length
            var originalDataLength = data2[2] + (data2[3] << 8);

            // skip first 4 bytes (PayBySquare qr code header + original data length)
            var payload = this.Decompress(data2, 4, originalDataLength);

            // first 4 bytes is crc32
            var crc = new Crc32().ComputeHash(payload, 4, originalDataLength - 4);

            Array.Reverse(crc);

            if (BitConverter.ToInt32(crc, 0) != BitConverter.ToInt32(payload, 0))
            {
                throw new InvalidDataException("Checksum of decompressed data does not match.");
            }

            return(payload);
        }