├── .gitignore ├── JsonHashing.Handlers ├── Hasher.cs ├── JsonHashing.Handlers.csproj └── Serializer.cs ├── JsonHashing.WebApi ├── .config │ └── dotnet-tools.json ├── Controllers │ └── InvoiceHasher.cs ├── JsonHashing.WebApi.csproj ├── Program.cs ├── Properties │ ├── ServiceDependencies │ │ └── ETASerialize - Web Deploy │ │ │ └── profile.arm.json │ └── launchSettings.json ├── Startup.cs ├── TokenRSA.cs ├── appsettings.Development.json └── appsettings.json ├── JsonHashing.sln └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Aa][Rr][Mm]/ 27 | [Aa][Rr][Mm]64/ 28 | bld/ 29 | [Bb]in/ 30 | [Oo]bj/ 31 | [Ll]og/ 32 | [Ll]ogs/ 33 | 34 | # Visual Studio 2015/2017 cache/options directory 35 | .vs/ 36 | # Uncomment if you have tasks that create the project's static files in wwwroot 37 | #wwwroot/ 38 | 39 | # Visual Studio 2017 auto generated files 40 | Generated\ Files/ 41 | 42 | # MSTest test Results 43 | [Tt]est[Rr]esult*/ 44 | [Bb]uild[Ll]og.* 45 | 46 | # NUnit 47 | *.VisualState.xml 48 | TestResult.xml 49 | nunit-*.xml 50 | 51 | # Build Results of an ATL Project 52 | [Dd]ebugPS/ 53 | [Rr]eleasePS/ 54 | dlldata.c 55 | 56 | # Benchmark Results 57 | BenchmarkDotNet.Artifacts/ 58 | 59 | # .NET Core 60 | project.lock.json 61 | project.fragment.lock.json 62 | artifacts/ 63 | 64 | # StyleCop 65 | StyleCopReport.xml 66 | 67 | # Files built by Visual Studio 68 | *_i.c 69 | *_p.c 70 | *_h.h 71 | *.ilk 72 | *.meta 73 | *.obj 74 | *.iobj 75 | *.pch 76 | *.pdb 77 | *.ipdb 78 | *.pgc 79 | *.pgd 80 | *.rsp 81 | *.sbr 82 | *.tlb 83 | *.tli 84 | *.tlh 85 | *.tmp 86 | *.tmp_proj 87 | *_wpftmp.csproj 88 | *.log 89 | *.vspscc 90 | *.vssscc 91 | .builds 92 | *.pidb 93 | *.svclog 94 | *.scc 95 | 96 | # Chutzpah Test files 97 | _Chutzpah* 98 | 99 | # Visual C++ cache files 100 | ipch/ 101 | *.aps 102 | *.ncb 103 | *.opendb 104 | *.opensdf 105 | *.sdf 106 | *.cachefile 107 | *.VC.db 108 | *.VC.VC.opendb 109 | 110 | # Visual Studio profiler 111 | *.psess 112 | *.vsp 113 | *.vspx 114 | *.sap 115 | 116 | # Visual Studio Trace Files 117 | *.e2e 118 | 119 | # TFS 2012 Local Workspace 120 | $tf/ 121 | 122 | # Guidance Automation Toolkit 123 | *.gpState 124 | 125 | # ReSharper is a .NET coding add-in 126 | _ReSharper*/ 127 | *.[Rr]e[Ss]harper 128 | *.DotSettings.user 129 | 130 | # TeamCity is a build add-in 131 | _TeamCity* 132 | 133 | # DotCover is a Code Coverage Tool 134 | *.dotCover 135 | 136 | # AxoCover is a Code Coverage Tool 137 | .axoCover/* 138 | !.axoCover/settings.json 139 | 140 | # Visual Studio code coverage results 141 | *.coverage 142 | *.coveragexml 143 | 144 | # NCrunch 145 | _NCrunch_* 146 | .*crunch*.local.xml 147 | nCrunchTemp_* 148 | 149 | # MightyMoose 150 | *.mm.* 151 | AutoTest.Net/ 152 | 153 | # Web workbench (sass) 154 | .sass-cache/ 155 | 156 | # Installshield output folder 157 | [Ee]xpress/ 158 | 159 | # DocProject is a documentation generator add-in 160 | DocProject/buildhelp/ 161 | DocProject/Help/*.HxT 162 | DocProject/Help/*.HxC 163 | DocProject/Help/*.hhc 164 | DocProject/Help/*.hhk 165 | DocProject/Help/*.hhp 166 | DocProject/Help/Html2 167 | DocProject/Help/html 168 | 169 | # Click-Once directory 170 | publish/ 171 | 172 | # Publish Web Output 173 | *.[Pp]ublish.xml 174 | *.azurePubxml 175 | # Note: Comment the next line if you want to checkin your web deploy settings, 176 | # but database connection strings (with potential passwords) will be unencrypted 177 | *.pubxml 178 | *.publishproj 179 | 180 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 181 | # checkin your Azure Web App publish settings, but sensitive information contained 182 | # in these scripts will be unencrypted 183 | PublishScripts/ 184 | 185 | # NuGet Packages 186 | *.nupkg 187 | # NuGet Symbol Packages 188 | *.snupkg 189 | # The packages folder can be ignored because of Package Restore 190 | **/[Pp]ackages/* 191 | # except build/, which is used as an MSBuild target. 192 | !**/[Pp]ackages/build/ 193 | # Uncomment if necessary however generally it will be regenerated when needed 194 | #!**/[Pp]ackages/repositories.config 195 | # NuGet v3's project.json files produces more ignorable files 196 | *.nuget.props 197 | *.nuget.targets 198 | 199 | # Microsoft Azure Build Output 200 | csx/ 201 | *.build.csdef 202 | 203 | # Microsoft Azure Emulator 204 | ecf/ 205 | rcf/ 206 | 207 | # Windows Store app package directories and files 208 | AppPackages/ 209 | BundleArtifacts/ 210 | Package.StoreAssociation.xml 211 | _pkginfo.txt 212 | *.appx 213 | *.appxbundle 214 | *.appxupload 215 | 216 | # Visual Studio cache files 217 | # files ending in .cache can be ignored 218 | *.[Cc]ache 219 | # but keep track of directories ending in .cache 220 | !?*.[Cc]ache/ 221 | 222 | # Others 223 | ClientBin/ 224 | ~$* 225 | *~ 226 | *.dbmdl 227 | *.dbproj.schemaview 228 | *.jfm 229 | *.pfx 230 | *.publishsettings 231 | orleans.codegen.cs 232 | 233 | # Including strong name files can present a security risk 234 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 235 | #*.snk 236 | 237 | # Since there are multiple workflows, uncomment next line to ignore bower_components 238 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 239 | #bower_components/ 240 | 241 | # RIA/Silverlight projects 242 | Generated_Code/ 243 | 244 | # Backup & report files from converting an old project file 245 | # to a newer Visual Studio version. Backup files are not needed, 246 | # because we have git ;-) 247 | _UpgradeReport_Files/ 248 | Backup*/ 249 | UpgradeLog*.XML 250 | UpgradeLog*.htm 251 | ServiceFabricBackup/ 252 | *.rptproj.bak 253 | 254 | # SQL Server files 255 | *.mdf 256 | *.ldf 257 | *.ndf 258 | 259 | # Business Intelligence projects 260 | *.rdl.data 261 | *.bim.layout 262 | *.bim_*.settings 263 | *.rptproj.rsuser 264 | *- [Bb]ackup.rdl 265 | *- [Bb]ackup ([0-9]).rdl 266 | *- [Bb]ackup ([0-9][0-9]).rdl 267 | 268 | # Microsoft Fakes 269 | FakesAssemblies/ 270 | 271 | # GhostDoc plugin setting file 272 | *.GhostDoc.xml 273 | 274 | # Node.js Tools for Visual Studio 275 | .ntvs_analysis.dat 276 | node_modules/ 277 | 278 | # Visual Studio 6 build log 279 | *.plg 280 | 281 | # Visual Studio 6 workspace options file 282 | *.opt 283 | 284 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 285 | *.vbw 286 | 287 | # Visual Studio LightSwitch build output 288 | **/*.HTMLClient/GeneratedArtifacts 289 | **/*.DesktopClient/GeneratedArtifacts 290 | **/*.DesktopClient/ModelManifest.xml 291 | **/*.Server/GeneratedArtifacts 292 | **/*.Server/ModelManifest.xml 293 | _Pvt_Extensions 294 | 295 | # Paket dependency manager 296 | .paket/paket.exe 297 | paket-files/ 298 | 299 | # FAKE - F# Make 300 | .fake/ 301 | 302 | # CodeRush personal settings 303 | .cr/personal 304 | 305 | # Python Tools for Visual Studio (PTVS) 306 | __pycache__/ 307 | *.pyc 308 | 309 | # Cake - Uncomment if you are using it 310 | # tools/** 311 | # !tools/packages.config 312 | 313 | # Tabs Studio 314 | *.tss 315 | 316 | # Telerik's JustMock configuration file 317 | *.jmconfig 318 | 319 | # BizTalk build output 320 | *.btp.cs 321 | *.btm.cs 322 | *.odx.cs 323 | *.xsd.cs 324 | 325 | # OpenCover UI analysis results 326 | OpenCover/ 327 | 328 | # Azure Stream Analytics local run output 329 | ASALocalRun/ 330 | 331 | # MSBuild Binary and Structured Log 332 | *.binlog 333 | 334 | # NVidia Nsight GPU debugger configuration file 335 | *.nvuser 336 | 337 | # MFractors (Xamarin productivity tool) working folder 338 | .mfractor/ 339 | 340 | # Local History for Visual Studio 341 | .localhistory/ 342 | 343 | # BeatPulse healthcheck temp database 344 | healthchecksdb 345 | 346 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 347 | MigrationBackup/ 348 | 349 | # Ionide (cross platform F# VS Code tools) working folder 350 | .ionide/ 351 | -------------------------------------------------------------------------------- /JsonHashing.Handlers/Hasher.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Security.Cryptography; 4 | using System.Text; 5 | 6 | namespace JsonHashing.Handlers 7 | { 8 | public class Hasher 9 | { 10 | public byte[] Hash(string input) 11 | { 12 | using (SHA256 sha = SHA256.Create()) 13 | { 14 | var output = sha.ComputeHash(Encoding.UTF8.GetBytes(input)); 15 | return output; 16 | } 17 | } 18 | 19 | public byte[] HashBytes(byte[] input) 20 | { 21 | using (SHA256 sha = SHA256.Create()) 22 | { 23 | var output = sha.ComputeHash(input); 24 | return output; 25 | } 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /JsonHashing.Handlers/JsonHashing.Handlers.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /JsonHashing.Handlers/Serializer.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * This sample was created by Mohammed S. Elsuissey 3 | * Software consultant and .Net developer 4 | * asegypt@gmail.com 5 | * 01000592036 6 | */ 7 | using Newtonsoft.Json; 8 | using Newtonsoft.Json.Linq; 9 | using System; 10 | using System.Collections.Generic; 11 | using System.Linq; 12 | using System.Text; 13 | 14 | namespace JsonHashing.Handlers 15 | { 16 | public class Serializer 17 | { 18 | public string Serialize(JObject request) 19 | { 20 | return SerializeToken(request); 21 | } 22 | 23 | private string SerializeToken(JToken request) 24 | { 25 | string serialized = ""; 26 | if (request.Parent is null) 27 | { 28 | SerializeToken(request.First); 29 | } 30 | else 31 | { 32 | if (request.Type == JTokenType.Property) 33 | { 34 | string name = ((JProperty)request).Name.ToUpper(); 35 | serialized += "\"" + name + "\""; 36 | foreach (var property in request) 37 | { 38 | if (property.Type == JTokenType.Object) 39 | { 40 | serialized += SerializeToken(property); 41 | } 42 | if (property.Type == JTokenType.Boolean || property.Type == JTokenType.Integer || property.Type == JTokenType.Float || property.Type == JTokenType.Date) 43 | { 44 | serialized += "\"" + property.Value() + "\""; 45 | } 46 | if (property.Type == JTokenType.String) 47 | { 48 | serialized += JsonConvert.ToString(property.Value()); 49 | } 50 | if (property.Type == JTokenType.Array) 51 | { 52 | foreach (var item in property.Children()) 53 | { 54 | serialized += "\"" + ((JProperty)request).Name.ToUpper() + "\""; 55 | if (item.Type == JTokenType.String) 56 | { 57 | serialized += JsonConvert.ToString(item.Value()); 58 | } 59 | else 60 | { 61 | serialized += SerializeToken(item); 62 | } 63 | } 64 | } 65 | } 66 | } 67 | } 68 | if (request.Type == JTokenType.Object) 69 | { 70 | foreach (var property in request.Children()) 71 | { 72 | 73 | if (property.Type == JTokenType.Object || property.Type == JTokenType.Property) 74 | { 75 | serialized += SerializeToken(property); 76 | } 77 | } 78 | } 79 | 80 | return serialized; 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /JsonHashing.WebApi/.config/dotnet-tools.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": 1, 3 | "isRoot": true, 4 | "tools": { 5 | "dotnet-ef": { 6 | "version": "5.0.0", 7 | "commands": [ 8 | "dotnet-ef" 9 | ] 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /JsonHashing.WebApi/Controllers/InvoiceHasher.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * This sample was created by Mohammed S. Elsuissey 3 | * Software consultant and .Net developer 4 | * asegypt@gmail.com 5 | * 01000592036 6 | */ 7 | using JsonHashing.Handlers; 8 | using Microsoft.AspNetCore.Mvc; 9 | using Microsoft.Extensions.Configuration; 10 | using Net.Pkcs11Interop.Common; 11 | using Net.Pkcs11Interop.HighLevelAPI; 12 | using Newtonsoft.Json; 13 | using Newtonsoft.Json.Linq; 14 | using Org.BouncyCastle.Asn1; 15 | using Org.BouncyCastle.Asn1.Ess; 16 | using System; 17 | using System.Collections.Generic; 18 | using System.IO; 19 | using System.Linq; 20 | using System.Security.Cryptography; 21 | using System.Security.Cryptography.Pkcs; 22 | using System.Security.Cryptography.X509Certificates; 23 | using System.Text; 24 | using System.Threading.Tasks; 25 | 26 | namespace JsonHashing.WebApi.Controllers 27 | { 28 | [Route("api/[controller]")] 29 | [ApiController] 30 | public class InvoiceHasher : ControllerBase 31 | { 32 | private readonly Serializer _serializer; 33 | private readonly Hasher _hasher; 34 | private readonly IConfiguration _configuration; 35 | 36 | 37 | private readonly string DllLibPath = "eps2003csp11.dll"; 38 | 39 | private string TokenBin; 40 | 41 | public InvoiceHasher(Serializer serializer, Hasher hasher, IConfiguration configuration) 42 | { 43 | _serializer = serializer; 44 | _hasher = hasher; 45 | _configuration = configuration; 46 | TokenBin = _configuration["TokenBin"]; 47 | } 48 | 49 | [HttpPost("[action]")] 50 | public async Task Serialize() 51 | { 52 | using (StreamReader sr = new StreamReader(Request.Body)) 53 | { 54 | string requestbody = await sr.ReadToEndAsync(); 55 | JObject request = JsonConvert.DeserializeObject(requestbody, new JsonSerializerSettings() 56 | { 57 | FloatFormatHandling = FloatFormatHandling.String, 58 | FloatParseHandling = FloatParseHandling.Decimal, 59 | DateFormatHandling = DateFormatHandling.IsoDateFormat, 60 | DateParseHandling = DateParseHandling.None 61 | }); 62 | var h = _serializer.Serialize(request); 63 | return h; 64 | }; 65 | } 66 | 67 | [HttpPost("[action]")] 68 | public async Task> Hash() 69 | { 70 | using (StreamReader sr = new StreamReader(Request.Body)) 71 | { 72 | string requestbody = await sr.ReadToEndAsync(); 73 | //var hashed = _hasher.Hash(requestbody); 74 | 75 | return Ok(SignWithCMS(Encoding.UTF8.GetBytes(requestbody))); 76 | }; 77 | } 78 | 79 | [HttpGet("[action]")] 80 | public ActionResult GetAllTokenDetails() 81 | { 82 | Pkcs11InteropFactories factories = new Pkcs11InteropFactories(); 83 | List tokens = new List(); 84 | List slots = new List(); 85 | using (IPkcs11Library pkcs11Library = factories.Pkcs11LibraryFactory.LoadPkcs11Library(factories, DllLibPath, AppType.MultiThreaded)) 86 | { 87 | var slotList = pkcs11Library.GetSlotList(SlotsType.WithTokenPresent).ToList(); 88 | slotList.ForEach(item => 89 | { 90 | tokens.Add(item.GetTokenInfo()); 91 | slots.Add(item.GetSlotInfo()); 92 | }); 93 | 94 | return Ok(new 95 | { 96 | tokens, 97 | slots 98 | }); 99 | } 100 | } 101 | 102 | [HttpPost("[action]")] 103 | public async Task> GetReceiptUUID() 104 | { 105 | using (StreamReader sr = new StreamReader(Request.Body)) 106 | { 107 | string requestbody = await sr.ReadToEndAsync(); 108 | JObject request = JsonConvert.DeserializeObject(requestbody, new JsonSerializerSettings() 109 | { 110 | FloatFormatHandling = FloatFormatHandling.String, 111 | FloatParseHandling = FloatParseHandling.Decimal, 112 | DateFormatHandling = DateFormatHandling.IsoDateFormat, 113 | DateParseHandling = DateParseHandling.None 114 | }); 115 | var serialized = _serializer.Serialize(request); 116 | var hashed = _hasher.Hash(serialized); 117 | var uuid = string.Join(string.Empty, Array.ConvertAll(hashed, b => b.ToString("x2"))); 118 | return uuid; 119 | }; 120 | } 121 | 122 | [HttpPost("[action]/{pin}")] 123 | public async Task> SignDocument([FromRoute] string pin) 124 | { 125 | this.TokenBin = pin; 126 | using (StreamReader sr = new StreamReader(Request.Body)) 127 | { 128 | string requestbody = await sr.ReadToEndAsync(); 129 | JObject request = JsonConvert.DeserializeObject(requestbody, new JsonSerializerSettings() 130 | { 131 | FloatFormatHandling = FloatFormatHandling.String, 132 | FloatParseHandling = FloatParseHandling.Decimal, 133 | DateFormatHandling = DateFormatHandling.IsoDateFormat, 134 | DateParseHandling = DateParseHandling.None 135 | }); 136 | var documents = request["documents"].ToObject(); 137 | 138 | var document = documents.FirstOrDefault().ToObject(); 139 | var serializedString = _serializer.Serialize(document); 140 | 141 | 142 | var signatureString = SignWithCMS(Encoding.UTF8.GetBytes(serializedString)); 143 | 144 | var signatures = new List(); 145 | signatures.Add(new ETASignature 146 | { 147 | signatureType = "I", 148 | value = signatureString 149 | }); 150 | document.Add("signatures", JArray.FromObject(signatures)); 151 | documents.Clear(); 152 | documents.Add(document); 153 | request.Remove("documents"); 154 | request.Add("documents", documents); 155 | return Ok(request.ToString()); 156 | } 157 | } 158 | [HttpGet] 159 | public ActionResult GetAllCerts() 160 | { 161 | Pkcs11InteropFactories factories = new Pkcs11InteropFactories(); 162 | using (IPkcs11Library pkcs11Library = factories.Pkcs11LibraryFactory.LoadPkcs11Library(factories, DllLibPath, AppType.MultiThreaded)) 163 | { 164 | ISlot slot = pkcs11Library.GetSlotList(SlotsType.WithTokenPresent).FirstOrDefault(); 165 | 166 | if (slot is null) 167 | { 168 | return Ok("No slots found"); 169 | } 170 | 171 | 172 | 173 | ITokenInfo tokenInfo = slot.GetTokenInfo(); 174 | 175 | ISlotInfo slotInfo = slot.GetSlotInfo(); 176 | 177 | using (var session = slot.OpenSession(SessionType.ReadWrite)) 178 | { 179 | session.Login(CKU.CKU_USER, Encoding.UTF8.GetBytes(TokenBin)); 180 | 181 | 182 | var certificateSearchAttributes = new List() 183 | { 184 | session.Factories.ObjectAttributeFactory.Create(CKA.CKA_CLASS, CKO.CKO_CERTIFICATE), 185 | session.Factories.ObjectAttributeFactory.Create(CKA.CKA_TOKEN, true), 186 | session.Factories.ObjectAttributeFactory.Create(CKA.CKA_CERTIFICATE_TYPE, CKC.CKC_X_509) 187 | }; 188 | 189 | IObjectHandle certificate = session.FindAllObjects(certificateSearchAttributes).FirstOrDefault(); 190 | 191 | var certificateValue = session.GetAttributeValue(certificate, new List 192 | { 193 | CKA.CKA_VALUE 194 | }); 195 | 196 | 197 | var xcert = new X509Certificate2(certificateValue[0].GetValueAsByteArray()); 198 | 199 | return Ok( 200 | 201 | new 202 | { 203 | xcert.Thumbprint, 204 | xcert.Subject, 205 | xcert.IssuerName, 206 | hasKeyNull = xcert.PrivateKey is null 207 | }); 208 | 209 | if (certificate is null) 210 | { 211 | return Ok("Certificate not found"); 212 | } 213 | JArray output = new JArray(); 214 | foreach (string location in Enum.GetNames(typeof(StoreLocation))) 215 | { 216 | foreach (string name in Enum.GetNames(typeof(StoreName))) 217 | { 218 | using (var store = new X509Store(Enum.Parse(name), Enum.Parse(location))) 219 | { 220 | store.Open(OpenFlags.MaxAllowed); 221 | foreach (var cert in store.Certificates.Find(X509FindType.FindByIssuerName, "Egypt Trust Sealing CA", true)) 222 | { 223 | output.Add(JObject.FromObject(new 224 | { 225 | location, 226 | name, 227 | cert.IssuerName.Name, 228 | cert.FriendlyName, 229 | Privatekey = cert.PrivateKey == null 230 | })); 231 | } 232 | store.Close(); 233 | } 234 | } 235 | } 236 | return Ok(output.ToString()); 237 | } 238 | } 239 | } 240 | private string SignWithCMS(byte[] data) 241 | { 242 | Pkcs11InteropFactories factories = new Pkcs11InteropFactories(); 243 | using (IPkcs11Library pkcs11Library = factories.Pkcs11LibraryFactory.LoadPkcs11Library(factories, DllLibPath, AppType.MultiThreaded)) 244 | { 245 | ISlot slot = pkcs11Library.GetSlotList(SlotsType.WithTokenPresent).FirstOrDefault(); 246 | 247 | if (slot is null) 248 | { 249 | return "No slots found"; 250 | } 251 | 252 | var token = slot.GetTokenInfo(); 253 | var subfi = slot.GetSlotInfo(); 254 | 255 | using (var session = slot.OpenSession(SessionType.ReadWrite)) 256 | { 257 | 258 | session.Login(CKU.CKU_USER, Encoding.UTF8.GetBytes(TokenBin)); 259 | 260 | var searchAttribute = new List() 261 | { 262 | session.Factories.ObjectAttributeFactory.Create(CKA.CKA_CLASS, CKO.CKO_CERTIFICATE), 263 | session.Factories.ObjectAttributeFactory.Create(CKA.CKA_TOKEN, true), 264 | session.Factories.ObjectAttributeFactory.Create(CKA.CKA_CERTIFICATE_TYPE, CKC.CKC_X_509) 265 | }; 266 | 267 | IObjectHandle certificate = session.FindAllObjects(searchAttribute).FirstOrDefault(); 268 | 269 | if (certificate is null) 270 | { 271 | return "Certificate not found"; 272 | } 273 | 274 | var attributeValues = session.GetAttributeValue(certificate, new List 275 | { 276 | CKA.CKA_VALUE 277 | }); 278 | 279 | 280 | var xcert = new X509Certificate2(attributeValues[0].GetValueAsByteArray()); 281 | 282 | searchAttribute = new List() 283 | { 284 | session.Factories.ObjectAttributeFactory.Create(CKA.CKA_CLASS, CKO.CKO_PRIVATE_KEY), 285 | session.Factories.ObjectAttributeFactory.Create(CKA.CKA_KEY_TYPE,CKK.CKK_RSA) 286 | }; 287 | 288 | IObjectHandle privateKeyHandler = session.FindAllObjects(searchAttribute).LastOrDefault(); 289 | 290 | 291 | 292 | RSA privateKey = new TokenRSA(xcert, session, slot, privateKeyHandler); 293 | 294 | ContentInfo content = new ContentInfo(new Oid("1.2.840.113549.1.7.5"), data); 295 | 296 | 297 | SignedCms cms = new SignedCms(content, true); 298 | 299 | 300 | EssCertIDv2 bouncyCertificate = new EssCertIDv2(new Org.BouncyCastle.Asn1.X509.AlgorithmIdentifier(new DerObjectIdentifier("1.2.840.113549.1.9.16.2.47")), _hasher.HashBytes(xcert.RawData)); 301 | 302 | var x = bouncyCertificate.HashAlgorithm; 303 | 304 | SigningCertificateV2 signerCertificateV2 = new SigningCertificateV2(new EssCertIDv2[] { bouncyCertificate }); 305 | 306 | CmsSigner signer = new CmsSigner(xcert); 307 | 308 | signer.PrivateKey = privateKey; 309 | 310 | signer.DigestAlgorithm = new Oid("2.16.840.1.101.3.4.2.1"); 311 | 312 | 313 | signer.SignedAttributes.Add(new Pkcs9SigningTime(DateTime.UtcNow)); 314 | signer.SignedAttributes.Add(new AsnEncodedData(new Oid("1.2.840.113549.1.9.16.2.47"), signerCertificateV2.GetEncoded())); 315 | 316 | cms.ComputeSignature(signer); 317 | 318 | var output = cms.Encode(); 319 | 320 | return Convert.ToBase64String(output); 321 | } 322 | } 323 | 324 | } 325 | 326 | } 327 | 328 | class ETASignature 329 | { 330 | public string signatureType { get; set; } 331 | 332 | public string value { get; set; } 333 | } 334 | } 335 | -------------------------------------------------------------------------------- /JsonHashing.WebApi/JsonHashing.WebApi.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 4ba1e10d-ae82-4c47-8dd1-52d34cdee32a 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | Always 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /JsonHashing.WebApi/Program.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * This sample was created by Mohammed S. Elsuissey 3 | * Software consultant and .Net developer 4 | * asegypt@gmail.com 5 | * 01000592036 6 | */ 7 | using System; 8 | using System.Collections.Generic; 9 | using System.Linq; 10 | using System.Threading.Tasks; 11 | using Microsoft.AspNetCore.Hosting; 12 | using Microsoft.Extensions.Configuration; 13 | using Microsoft.Extensions.Hosting; 14 | using Microsoft.Extensions.Logging; 15 | 16 | namespace JsonHashing.WebApi 17 | { 18 | public class Program 19 | { 20 | public static void Main(string[] args) 21 | { 22 | CreateHostBuilder(args).Build().Run(); 23 | } 24 | 25 | public static IHostBuilder CreateHostBuilder(string[] args) => 26 | Host.CreateDefaultBuilder(args) 27 | .ConfigureWebHostDefaults(webBuilder => 28 | { 29 | webBuilder.UseStartup(); 30 | }); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /JsonHashing.WebApi/Properties/ServiceDependencies/ETASerialize - Web Deploy/profile.arm.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#", 3 | "contentVersion": "1.0.0.0", 4 | "metadata": { 5 | "_dependencyType": "appService.windows" 6 | }, 7 | "parameters": { 8 | "resourceGroupName": { 9 | "type": "string", 10 | "defaultValue": "ETA", 11 | "metadata": { 12 | "description": "Name of the resource group for the resource. It is recommended to put resources under same resource group for better tracking." 13 | } 14 | }, 15 | "resourceGroupLocation": { 16 | "type": "string", 17 | "defaultValue": "centralus", 18 | "metadata": { 19 | "description": "Location of the resource group. Resource groups could have different location than resources, however by default we use API versions from latest hybrid profile which support all locations for resource types we support." 20 | } 21 | }, 22 | "resourceName": { 23 | "type": "string", 24 | "defaultValue": "ETASerialize", 25 | "metadata": { 26 | "description": "Name of the main resource to be created by this template." 27 | } 28 | }, 29 | "resourceLocation": { 30 | "type": "string", 31 | "defaultValue": "[parameters('resourceGroupLocation')]", 32 | "metadata": { 33 | "description": "Location of the resource. By default use resource group's location, unless the resource provider is not supported there." 34 | } 35 | } 36 | }, 37 | "variables": { 38 | "appServicePlan_name": "[concat('Plan', uniqueString(concat(parameters('resourceName'), subscription().subscriptionId)))]", 39 | "appServicePlan_ResourceId": "[concat('/subscriptions/', subscription().subscriptionId, '/resourceGroups/', parameters('resourceGroupName'), '/providers/Microsoft.Web/serverFarms/', variables('appServicePlan_name'))]" 40 | }, 41 | "resources": [ 42 | { 43 | "type": "Microsoft.Resources/resourceGroups", 44 | "name": "[parameters('resourceGroupName')]", 45 | "location": "[parameters('resourceGroupLocation')]", 46 | "apiVersion": "2019-10-01" 47 | }, 48 | { 49 | "type": "Microsoft.Resources/deployments", 50 | "name": "[concat(parameters('resourceGroupName'), 'Deployment', uniqueString(concat(parameters('resourceName'), subscription().subscriptionId)))]", 51 | "resourceGroup": "[parameters('resourceGroupName')]", 52 | "apiVersion": "2019-10-01", 53 | "dependsOn": [ 54 | "[parameters('resourceGroupName')]" 55 | ], 56 | "properties": { 57 | "mode": "Incremental", 58 | "template": { 59 | "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", 60 | "contentVersion": "1.0.0.0", 61 | "resources": [ 62 | { 63 | "location": "[parameters('resourceLocation')]", 64 | "name": "[parameters('resourceName')]", 65 | "type": "Microsoft.Web/sites", 66 | "apiVersion": "2015-08-01", 67 | "tags": { 68 | "[concat('hidden-related:', variables('appServicePlan_ResourceId'))]": "empty" 69 | }, 70 | "dependsOn": [ 71 | "[variables('appServicePlan_ResourceId')]" 72 | ], 73 | "kind": "app", 74 | "properties": { 75 | "name": "[parameters('resourceName')]", 76 | "kind": "app", 77 | "httpsOnly": true, 78 | "reserved": false, 79 | "serverFarmId": "[variables('appServicePlan_ResourceId')]", 80 | "siteConfig": { 81 | "metadata": [ 82 | { 83 | "name": "CURRENT_STACK", 84 | "value": "dotnetcore" 85 | } 86 | ] 87 | } 88 | }, 89 | "identity": { 90 | "type": "SystemAssigned" 91 | } 92 | }, 93 | { 94 | "location": "[parameters('resourceLocation')]", 95 | "name": "[variables('appServicePlan_name')]", 96 | "type": "Microsoft.Web/serverFarms", 97 | "apiVersion": "2015-08-01", 98 | "sku": { 99 | "name": "S1", 100 | "tier": "Standard", 101 | "family": "S", 102 | "size": "S1" 103 | }, 104 | "properties": { 105 | "name": "[variables('appServicePlan_name')]" 106 | } 107 | } 108 | ] 109 | } 110 | } 111 | } 112 | ] 113 | } -------------------------------------------------------------------------------- /JsonHashing.WebApi/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json.schemastore.org/launchsettings.json", 3 | "iisSettings": { 4 | "windowsAuthentication": false, 5 | "anonymousAuthentication": true, 6 | "iisExpress": { 7 | "applicationUrl": "http://localhost:56736", 8 | "sslPort": 44384 9 | } 10 | }, 11 | "profiles": { 12 | "IIS Express": { 13 | "commandName": "IISExpress", 14 | "launchBrowser": true, 15 | "launchUrl": "weatherforecast", 16 | "environmentVariables": { 17 | "ASPNETCORE_ENVIRONMENT": "Development" 18 | } 19 | }, 20 | "JsonHashing.WebApi": { 21 | "commandName": "Project", 22 | "launchBrowser": true, 23 | "launchUrl": "weatherforecast", 24 | "applicationUrl": "https://localhost:5001;http://localhost:5000", 25 | "environmentVariables": { 26 | "ASPNETCORE_ENVIRONMENT": "Development" 27 | } 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /JsonHashing.WebApi/Startup.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * This sample was created by Mohammed S. Elsuissey 3 | * Software consultant and .Net developer 4 | * asegypt@gmail.com 5 | * 01000592036 6 | */ 7 | using System; 8 | using System.Collections.Generic; 9 | using System.Linq; 10 | using System.Threading.Tasks; 11 | using JsonHashing.Handlers; 12 | using Microsoft.AspNetCore.Builder; 13 | using Microsoft.AspNetCore.Hosting; 14 | using Microsoft.AspNetCore.HttpsPolicy; 15 | using Microsoft.AspNetCore.Mvc; 16 | using Microsoft.Extensions.Configuration; 17 | using Microsoft.Extensions.DependencyInjection; 18 | using Microsoft.Extensions.Hosting; 19 | using Microsoft.Extensions.Logging; 20 | using Microsoft.OpenApi.Models; 21 | 22 | namespace JsonHashing.WebApi 23 | { 24 | public class Startup 25 | { 26 | public Startup(IConfiguration configuration) 27 | { 28 | Configuration = configuration; 29 | } 30 | 31 | public IConfiguration Configuration { get; } 32 | 33 | // This method gets called by the runtime. Use this method to add services to the container. 34 | public void ConfigureServices(IServiceCollection services) 35 | { 36 | services.AddMvc(); 37 | 38 | services.AddSwaggerGen(c => 39 | { 40 | c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" }); 41 | }); 42 | services.AddScoped(); 43 | services.AddScoped(); 44 | services.AddControllers(); 45 | } 46 | 47 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 48 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env) 49 | { 50 | if (env.IsDevelopment()) 51 | { 52 | app.UseDeveloperExceptionPage(); 53 | } 54 | 55 | app.UseHttpsRedirection(); 56 | 57 | app.UseRouting(); 58 | 59 | app.UseSwagger(); 60 | app.UseSwaggerUI(c => 61 | { 62 | c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); 63 | }); 64 | 65 | app.UseAuthorization(); 66 | 67 | app.UseEndpoints(endpoints => 68 | { 69 | endpoints.MapControllers(); 70 | }); 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /JsonHashing.WebApi/TokenRSA.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-2018 The Pkcs11Interop Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Written for the Pkcs11Interop project by: 19 | * Jaroslav IMRICH 20 | */ 21 | 22 | using Net.Pkcs11Interop.Common; 23 | using Net.Pkcs11Interop.HighLevelAPI; 24 | using Net.Pkcs11Interop.HighLevelAPI.Factories; 25 | using Net.Pkcs11Interop.HighLevelAPI.MechanismParams; 26 | using System; 27 | using System.Collections.Generic; 28 | using System.Linq; 29 | using System.Security.Cryptography; 30 | using System.Security.Cryptography.X509Certificates; 31 | using System.Threading.Tasks; 32 | 33 | namespace JsonHashing.WebApi 34 | { 35 | public class TokenRSA : RSA 36 | { 37 | private readonly X509Certificate2 _certificate; 38 | private readonly ISession _session; 39 | private readonly ISlot _slot; 40 | private readonly IObjectHandle _privateKeyHandle; 41 | 42 | public TokenRSA(X509Certificate2 certificate, ISession session, ISlot slot, IObjectHandle privateKeyHandle) 43 | { 44 | _certificate = certificate; 45 | _session = session; 46 | _slot = slot; 47 | _privateKeyHandle = privateKeyHandle; 48 | } 49 | 50 | 51 | public override byte[] SignHash(byte[] hash, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding) 52 | { 53 | if (hash == null || hash.Length == 0) 54 | throw new ArgumentNullException(nameof(hash)); 55 | 56 | if (hashAlgorithm == null) 57 | throw new ArgumentNullException(nameof(hashAlgorithm)); 58 | 59 | if (padding == null) 60 | throw new ArgumentNullException(nameof(padding)); 61 | 62 | if (padding == RSASignaturePadding.Pkcs1) 63 | { 64 | byte[] pkcs1DigestInfo = CreatePkcs1DigestInfo(hash, hashAlgorithm); 65 | if (pkcs1DigestInfo == null) 66 | throw new NotSupportedException(string.Format("Algorithm {0} is not supported", hashAlgorithm.Name)); 67 | 68 | 69 | using (IMechanism mechanism = _session.Factories.MechanismFactory.Create(CKM.CKM_RSA_PKCS)) 70 | { 71 | return _session.Sign(mechanism, _privateKeyHandle, pkcs1DigestInfo); 72 | } 73 | } 74 | else if (padding == RSASignaturePadding.Pss) 75 | { 76 | IMechanismParamsFactory mechanismParamsFactory = _slot.Factories.MechanismParamsFactory; 77 | 78 | ICkRsaPkcsPssParams pssMechanismParams = CreateCkRsaPkcsPssParams(mechanismParamsFactory, hash, hashAlgorithm); 79 | if (pssMechanismParams == null) 80 | throw new NotSupportedException(string.Format("Algorithm {0} is not supported", hashAlgorithm.Name)); 81 | 82 | 83 | using (IMechanism mechanism = _session.Factories.MechanismFactory.Create(CKM.CKM_RSA_PKCS_PSS, pssMechanismParams)) 84 | { 85 | 86 | return _session.Sign(mechanism, _privateKeyHandle, hash); 87 | } 88 | } 89 | else 90 | { 91 | throw new NotSupportedException(string.Format("Padding {0} is not supported", padding)); 92 | } 93 | } 94 | 95 | 96 | public override RSAParameters ExportParameters(bool includePrivateParameters) 97 | { 98 | if (includePrivateParameters) 99 | throw new NotSupportedException("Private key export is not supported"); 100 | 101 | RSA rsaPubKey = _certificate.GetRSAPublicKey(); 102 | return rsaPubKey.ExportParameters(false); 103 | } 104 | 105 | public override void ImportParameters(RSAParameters parameters) 106 | { 107 | throw new NotSupportedException("Key import is not supported"); 108 | } 109 | 110 | private static byte[] CreatePkcs1DigestInfo(byte[] hash, HashAlgorithmName hashAlgorithm) 111 | { 112 | if (hash == null || hash.Length == 0) 113 | throw new ArgumentNullException(nameof(hash)); 114 | 115 | byte[] pkcs1DigestInfo = null; 116 | 117 | if (hashAlgorithm == HashAlgorithmName.MD5) 118 | { 119 | if (hash.Length != 16) 120 | throw new ArgumentException("Invalid lenght of hash value"); 121 | 122 | pkcs1DigestInfo = new byte[] { 0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 123 | Array.Copy(hash, 0, pkcs1DigestInfo, pkcs1DigestInfo.Length - hash.Length, hash.Length); 124 | } 125 | else if (hashAlgorithm == HashAlgorithmName.SHA1) 126 | { 127 | if (hash.Length != 20) 128 | throw new ArgumentException("Invalid lenght of hash value"); 129 | 130 | pkcs1DigestInfo = new byte[] { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 131 | Array.Copy(hash, 0, pkcs1DigestInfo, pkcs1DigestInfo.Length - hash.Length, hash.Length); 132 | } 133 | else if (hashAlgorithm == HashAlgorithmName.SHA256) 134 | { 135 | if (hash.Length != 32) 136 | throw new ArgumentException("Invalid lenght of hash value"); 137 | 138 | pkcs1DigestInfo = new byte[] { 0x30, 0x31, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 139 | Array.Copy(hash, 0, pkcs1DigestInfo, pkcs1DigestInfo.Length - hash.Length, hash.Length); 140 | } 141 | else if (hashAlgorithm == HashAlgorithmName.SHA384) 142 | { 143 | if (hash.Length != 48) 144 | throw new ArgumentException("Invalid lenght of hash value"); 145 | 146 | pkcs1DigestInfo = new byte[] { 0x30, 0x41, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 147 | Array.Copy(hash, 0, pkcs1DigestInfo, pkcs1DigestInfo.Length - hash.Length, hash.Length); 148 | } 149 | else if (hashAlgorithm == HashAlgorithmName.SHA512) 150 | { 151 | if (hash.Length != 64) 152 | throw new ArgumentException("Invalid lenght of hash value"); 153 | 154 | pkcs1DigestInfo = new byte[] { 0x30, 0x51, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 155 | Array.Copy(hash, 0, pkcs1DigestInfo, pkcs1DigestInfo.Length - hash.Length, hash.Length); 156 | } 157 | 158 | return pkcs1DigestInfo; 159 | } 160 | 161 | 162 | private static ICkRsaPkcsPssParams CreateCkRsaPkcsPssParams(IMechanismParamsFactory mechanismParamsFactory, byte[] hash, HashAlgorithmName hashAlgorithm) 163 | { 164 | if (hash == null || hash.Length == 0) 165 | throw new ArgumentNullException(nameof(hash)); 166 | 167 | ICkRsaPkcsPssParams pssParams = null; 168 | 169 | if (hashAlgorithm == HashAlgorithmName.SHA1) 170 | { 171 | if (hash.Length != 20) 172 | throw new ArgumentException("Invalid lenght of hash value"); 173 | 174 | pssParams = mechanismParamsFactory.CreateCkRsaPkcsPssParams( 175 | hashAlg: (ulong)CKM.CKM_SHA_1, 176 | mgf: (ulong)CKG.CKG_MGF1_SHA1, 177 | len: (ulong)hash.Length 178 | ); 179 | } 180 | else if (hashAlgorithm == HashAlgorithmName.SHA256) 181 | { 182 | if (hash.Length != 32) 183 | throw new ArgumentException("Invalid lenght of hash value"); 184 | 185 | pssParams = mechanismParamsFactory.CreateCkRsaPkcsPssParams( 186 | hashAlg: (ulong)CKM.CKM_SHA256, 187 | mgf: (ulong)CKG.CKG_MGF1_SHA256, 188 | len: (ulong)hash.Length 189 | ); 190 | } 191 | else if (hashAlgorithm == HashAlgorithmName.SHA384) 192 | { 193 | if (hash.Length != 48) 194 | throw new ArgumentException("Invalid lenght of hash value"); 195 | 196 | pssParams = mechanismParamsFactory.CreateCkRsaPkcsPssParams( 197 | hashAlg: (ulong)CKM.CKM_SHA384, 198 | mgf: (ulong)CKG.CKG_MGF1_SHA384, 199 | len: (ulong)hash.Length 200 | ); 201 | } 202 | else if (hashAlgorithm == HashAlgorithmName.SHA512) 203 | { 204 | if (hash.Length != 64) 205 | throw new ArgumentException("Invalid lenght of hash value"); 206 | 207 | pssParams = mechanismParamsFactory.CreateCkRsaPkcsPssParams( 208 | hashAlg: (ulong)CKM.CKM_SHA512, 209 | mgf: (ulong)CKG.CKG_MGF1_SHA512, 210 | len: (ulong)hash.Length 211 | ); 212 | } 213 | 214 | return pssParams; 215 | } 216 | } 217 | } -------------------------------------------------------------------------------- /JsonHashing.WebApi/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft": "Warning", 6 | "Microsoft.Hosting.Lifetime": "Information" 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /JsonHashing.WebApi/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft": "Warning", 6 | "Microsoft.Hosting.Lifetime": "Information" 7 | } 8 | }, 9 | "StoreName": "AddressBook", 10 | "StoreLocation": "CurrentUser", 11 | "AllowedHosts": "*", 12 | "TokenBin": "123456" 13 | } 14 | -------------------------------------------------------------------------------- /JsonHashing.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.30621.155 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JsonHashing.WebApi", "JsonHashing.WebApi\JsonHashing.WebApi.csproj", "{D79196D5-B46D-4CC7-A5C0-F27512EBEEAE}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JsonHashing.Handlers", "JsonHashing.Handlers\JsonHashing.Handlers.csproj", "{96583CEF-773B-4583-8945-25C34B4E8900}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {D79196D5-B46D-4CC7-A5C0-F27512EBEEAE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {D79196D5-B46D-4CC7-A5C0-F27512EBEEAE}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {D79196D5-B46D-4CC7-A5C0-F27512EBEEAE}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {D79196D5-B46D-4CC7-A5C0-F27512EBEEAE}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {96583CEF-773B-4583-8945-25C34B4E8900}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {96583CEF-773B-4583-8945-25C34B4E8900}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {96583CEF-773B-4583-8945-25C34B4E8900}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {96583CEF-773B-4583-8945-25C34B4E8900}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {CB09E990-122D-4144-85B0-C19A24760FE5} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ETA Serialization tool 2 | this tool is for serializing the ETA invoices to it's serialized (ready to be signed) version 3 | 4 | This tool is developed by 5 | - Mohammed S. Elsuissey 6 | - Software consultant and dot net developer 7 | - asegypt@gmail.com 8 | - 01000592036 9 | 10 | --------------------------------------------------------------------------------