-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support to decode the raw QR Code data to a SMART Health Card J…
…WS token, see SmartHealthCardQRCodeDecoder
- Loading branch information
1 parent
55fa209
commit 15a3c1b
Showing
9 changed files
with
223 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using SmartHealthCard.QRCode.Model; | ||
using System.Collections.Generic; | ||
|
||
namespace SmartHealthCard.QRCode.Encoder | ||
{ | ||
public interface INumericalModeDecoder | ||
{ | ||
string Decode(IEnumerable<Chunk> ChunkList); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using SmartHealthCard.QRCode.Model; | ||
using System.Collections.Generic; | ||
|
||
namespace SmartHealthCard.QRCode.Encoder | ||
{ | ||
public interface IQRCodeDecoder | ||
{ | ||
IEnumerable<Chunk> GetQRCodeChunkList(IEnumerable<string> QRCodeRawDataList); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using SmartHealthCard.QRCode.Model; | ||
|
||
namespace SmartHealthCard.QRCode.Encoder | ||
{ | ||
public class NumericalModeDecoder : INumericalModeDecoder | ||
{ | ||
public string Decode(IEnumerable<Chunk> ChunkList) | ||
{ | ||
StringBuilder StringBuilder = new StringBuilder(); | ||
foreach (Chunk Chunk in ChunkList) | ||
{ | ||
string Numeric = Chunk.NumericSegment; | ||
foreach (string Number in Spliter(Numeric, 2)) | ||
{ | ||
if (int.TryParse(Number, out int IntNumber)) | ||
{ | ||
StringBuilder.Append(Convert.ToChar(IntNumber + 45)); | ||
} | ||
} | ||
} | ||
return StringBuilder.ToString(); | ||
} | ||
|
||
private IEnumerable<string> Spliter(string str, int chunkSize) | ||
{ | ||
return Enumerable.Range(0, str.Length / chunkSize) | ||
.Select(i => str.Substring(i * chunkSize, chunkSize)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using Net.Codecrete.QrCodeGenerator; | ||
using SmartHealthCard.QRCode.Exceptions; | ||
using SmartHealthCard.QRCode.Model; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Drawing; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace SmartHealthCard.QRCode.Encoder | ||
{ | ||
public class QRCodeDecoder : IQRCodeDecoder | ||
{ | ||
public IEnumerable<Chunk> GetQRCodeChunkList(IEnumerable<string> QRCodeRawDataList) | ||
{ | ||
//shc:/2/3/56762909524320603460292437404460<snipped for brevity> | ||
//shc:/56762909524320603460292437404460<snipped for brevity> | ||
List<Chunk> ChunkList = new List<Chunk>(); | ||
Chunk? Chunk = null; | ||
foreach (string QRCodeRawData in QRCodeRawDataList) | ||
{ | ||
string[] Split = QRCodeRawData.Split('/'); | ||
if (Split.Length == 2) | ||
{ | ||
Chunk = new Chunk($"{Split[0]}/", Split[1]); | ||
} | ||
else if (Split.Length == 4) | ||
{ | ||
Chunk = new Chunk($"{Split[0]}/{Split[1]}/{Split[2]}/", Split[3]); | ||
} | ||
else | ||
{ | ||
throw new QRCodeChunkFormatException($"The raw QR Code data was incorrectly formated, found {Split.Length} chunks where only 2 or 4 are allowed."); | ||
} | ||
ChunkList.Add(Chunk); | ||
} | ||
return ChunkList; | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
SmartHealthCard.QRCode/Exceptions/QRCodeChunkFormatException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace SmartHealthCard.QRCode.Exceptions | ||
{ | ||
public class QRCodeChunkFormatException : FormatException | ||
{ | ||
public QRCodeChunkFormatException(string message) : base(message) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using SmartHealthCard.QRCode.Encoder; | ||
using SmartHealthCard.QRCode.Model; | ||
using System.Collections.Generic; | ||
|
||
namespace SmartHealthCard.QRCode | ||
{ | ||
/// <summary> | ||
/// A decoder to decode from the raw QR Code data to a SMART Health Card JWS token | ||
/// Sorry but this does not accept a QR image directly, it requires a scanner to | ||
/// have gotten the raw QR Code data from the QR Code image | ||
/// </summary> | ||
public class SmartHealthCardQRCodeDecoder | ||
{ | ||
private readonly INumericalModeDecoder NumericalModeDecoder; | ||
private readonly IQRCodeDecoder QRCodeDecoder; | ||
|
||
/// <summary> | ||
/// Default Constructor | ||
/// </summary> | ||
public SmartHealthCardQRCodeDecoder() | ||
{ | ||
this.NumericalModeDecoder = new NumericalModeDecoder(); | ||
this.QRCodeDecoder = new QRCodeDecoder(); | ||
} | ||
|
||
/// <summary> | ||
/// Provide any implementation of the following interfaces to override their default implementation | ||
/// </summary> | ||
/// <param name="NumericalModeDecoder">Provides an implementation of the Numerical decoder used for SMART Health Card QR Codes, see: https://smarthealth.cards/#encoding-chunks-as-qr-codes </param> | ||
/// <param name="QRCodeDecoder">Provides an implementation of the chuck decoder used for SMART Health Card QR Codes, see: https://smarthealth.cards/#encoding-chunks-as-qr-codes</param> | ||
public SmartHealthCardQRCodeDecoder( | ||
INumericalModeDecoder? NumericalModeDecoder = null, | ||
IQRCodeDecoder? QRCodeDecoder = null) | ||
{ | ||
this.NumericalModeDecoder = NumericalModeDecoder ?? new NumericalModeDecoder(); | ||
this.QRCodeDecoder = QRCodeDecoder ?? new QRCodeDecoder(); | ||
} | ||
|
||
/// <summary> | ||
/// Provided SMART Health Card QR Code raw data and it will return a SMART Health Card JWS Token | ||
/// </summary> | ||
/// <param name="QRCodeRawDataList"></param> | ||
/// <returns></returns> | ||
public string GetToken(List<string> QRCodeRawDataList) | ||
{ | ||
IQRCodeDecoder QRCodeDecoder = new QRCodeDecoder(); | ||
IEnumerable<Chunk> ChunkList = QRCodeDecoder.GetQRCodeChunkList(QRCodeRawDataList); | ||
INumericalModeDecoder NumericalModeDecoder = new NumericalModeDecoder(); | ||
string test = NumericalModeDecoder.Decode(ChunkList); | ||
return test; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters