private IICType GetIICandIICSignature(RegisterInvoiceRequest invoiceRequest, X509Certificate2 certificate, string issuerTIN) { IICType iicType = null; if (invoiceRequest.Signature != null) { throw new Exception("Invoice already signed"); } if (invoiceRequest != null && invoiceRequest.Invoice.IIC == null && invoiceRequest.Invoice.IICSignature == null) { InvoiceType invoice = invoiceRequest.Invoice; IICGenerator iicTypeGenerator = new IICGenerator(certificate); IICConfig iicConfig = new IICConfig() { IssuerTIN = issuerTIN, DateTimeCreated = invoice.IssueDateTime.ToString(Fiscalization.DATE_FORMAT_LONG), InvoiceNumber = invoice.InvNum, BusinUnitCode = invoice.BusinUnitCode, TCRCode = invoice.TCRCode, SoftCode = invoice.SoftCode, TotalPrice = invoice.TotPrice.ToString() }; iicType = iicTypeGenerator.Generate(iicConfig); } return(iicType); }
private string GetInputParameterForIIC(IICConfig iicConfig) { StringBuilder inputParameter = new StringBuilder(); inputParameter.Append(iicConfig.IssuerTIN); inputParameter.Append("|" + iicConfig.DateTimeCreated); inputParameter.Append("|" + iicConfig.InvoiceNumber); inputParameter.Append("|" + iicConfig.BusinUnitCode); inputParameter.Append("|" + iicConfig.TCRCode); inputParameter.Append("|" + iicConfig.SoftCode); inputParameter.Append("|" + iicConfig.TotalPrice); return(inputParameter.ToString()); }
public static void ThrowExceptionIfNotValid(IICConfig iicConfig) { if (iicConfig == null) { throw new ArgumentNullException("IICConfig can't be null!"); } PropertyInfo[] iicConfigProperties = typeof(IICConfig).GetProperties(); foreach (PropertyInfo property in iicConfigProperties) { if (string.IsNullOrEmpty(property.GetValue(iicConfig)?.ToString())) { throw new ArgumentNullException($"IICConfig: {property.Name} property can't be null or empty!"); } } }
public IICType Generate(IICConfig iicConfig) { IICValidator.ThrowExceptionIfNotValid(iicConfig); var inputParameter = GetInputParameterForIIC(iicConfig); // Load a private from a key store RSA privateKey = _certificate.GetRSAPrivateKey(); IICType iicType = new IICType(); // Create IIC signature according to RSASSA-PKCS-v1_5 byte[] iicSignature = privateKey.SignData(Encoding.ASCII.GetBytes(inputParameter), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); iicType.IICSignature = BitConverter.ToString(iicSignature).Replace("-", string.Empty); // Hash IIC signature with MD5 to create IIC byte[] iic = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(iicSignature); iicType.IIC = BitConverter.ToString(iic).Replace("-", string.Empty); return(iicType); }