public EMHSignature(String Algorithm,
                     SignatureFormats Format,
                     String Value,
                     String PreviousValue = null)
 {
     this.Algorithm     = Algorithm;
     this.Format        = Format;
     this.Value         = Value;
     this.PreviousValue = PreviousValue;
 }
        public override ISignResult SignMeasurement(IMeasurementValue2 MeasurementValue,
                                                    Byte[]              PrivateKey,
                                                    SignatureFormats SignatureFormat = SignatureFormats.DER)
        {
            try
            {
                #region Check MeasurementValue

                if (MeasurementValue == null)
                {
                    return(new EMHSignResult(Status: SignResult.InvalidMeasurementValue));
                }

                if (!(MeasurementValue is IEMHMeasurementValue2 EMHMeasurementValue))
                {
                    return(new EMHSignResult(Status: SignResult.InvalidMeasurementValue));
                }

                #endregion

                #region Parse PrivateKey

                ECPrivateKeyParameters EMHPrivateKey = null;

                try
                {
                    EMHPrivateKey = ParsePrivateKey(PrivateKey);
                }
                catch (Exception e)
                {
                    return(new EMHSignResult(Status:        SignResult.InvalidPublicKey,
                                             ErrorMessage:  e.Message));
                }

                #endregion


                var cryptoBuffer = new Byte[320];

                var signResult = new EMHSignResult(

                    MeterId:                      cryptoBuffer.SetHex(EMHMeasurementValue.Measurement.MeterId, 0),
                    Timestamp:                    cryptoBuffer.SetTimestamp32(EMHMeasurementValue.Timestamp, 10),
                    InfoStatus:                   cryptoBuffer.SetHex(EMHMeasurementValue.InfoStatus, 14, false),
                    SecondsIndex:                 cryptoBuffer.SetUInt32(EMHMeasurementValue.SecondsIndex, 15, true),
                    PaginationId:                 cryptoBuffer.SetHex(EMHMeasurementValue.PaginationId, 19, true),
                    OBIS:                         cryptoBuffer.SetHex(EMHMeasurementValue.Measurement.OBIS, 23, false),
                    UnitEncoded:                  cryptoBuffer.SetInt8(EMHMeasurementValue.Measurement.UnitEncoded, 29),
                    Scale:                        cryptoBuffer.SetInt8(EMHMeasurementValue.Measurement.Scale, 30),
                    Value:                        cryptoBuffer.SetUInt64(EMHMeasurementValue.Value, 31, true),
                    LogBookIndex:                 cryptoBuffer.SetHex(EMHMeasurementValue.LogBookIndex, 39, false),
                    AuthorizationStart:           cryptoBuffer.SetText(EMHMeasurementValue.Measurement.ChargingSession.AuthorizationStart.Id, 41),
                    AuthorizationStartTimestamp:  cryptoBuffer.SetTimestamp32(EMHMeasurementValue.Measurement.ChargingSession.AuthorizationStart.Timestamp, 169),

                    Status:                       SignResult.InvalidMeasurementValue);


                var SHA256Hash = new Byte[0];

                // Only the first 24 bytes/192 bits are used!
                using (var SHA256 = new SHA256Managed())
                {
                    SHA256Hash = SHA256.ComputeHash(cryptoBuffer);

                    signResult.SHA256Value = SHA256Hash.ToHexString().
                                             Substring(0, 48);
                }

                var meter = GetMeter(MeasurementValue.Measurement.MeterId);
                if (meter != null)
                {
                    signResult.SetMeter(meter);

                    var publicKey = meter.PublicKeys.FirstOrDefault();
                    if (publicKey != null && (publicKey.Value?.Trim().IsNotNullOrEmpty() == true))
                    {
                        try
                        {
                            var EMHPublicKey = ParsePublicKey(publicKey.Value?.Trim());

                            //var bVerifier   = SignerUtilities.GetSigner("SHA-256withECDSA");
                            var signer = SignerUtilities.GetSigner("NONEwithECDSA");
                            signer.Init(true, EMHPrivateKey);
                            signer.BlockUpdate(SHA256Hash, 0, 24);

                            var signature = new EMHSignature(signer.AlgorithmName,
                                                             SignatureFormat, // ToDo: Fix signature format selection!
                                                             signer.GenerateSignature().ToHexString());

                            MeasurementValue.Signatures.Add(signature);
                            signResult.SetSignatureValue(SignResult.OK, signature);
                        }
                        catch (Exception e)
                        {
                            signResult.SetError(Status:        SignResult.InvalidPublicKey,
                                                ErrorMessage:  e.Message);
                        }
                    }
                    else
                    {
                        signResult.SetStatus(SignResult.PublicKeyNotFound);
                    }
                }
                else
                {
                    signResult.SetStatus(SignResult.MeterNotFound);
                }



                return(signResult);
            }
            catch (Exception e)
            {
                return(new EMHSignResult(Status:        SignResult.InvalidMeasurementValue,
                                         ErrorMessage:  e.Message));
            }
        }
 public override ISignResult SignChargingSession(IChargingSession ChargingSession,
                                                 Byte[]            PrivateKey,
                                                 SignatureFormats SignatureFormat = SignatureFormats.DER)
 {
     return(null);
 }
 public abstract ISignResult SignMeasurement(IMeasurementValue2 MeasurementValue,
                                             Byte[]              PrivateKey,
                                             SignatureFormats SignatureFormat = SignatureFormats.DER);
 public abstract ISignResult SignChargingSession(IChargingSession ChargingSession,
                                                 Byte[]              PrivateKey,
                                                 SignatureFormats SignatureFormat = SignatureFormats.DER);