/// <summary> /// 对待签名数据签名 /// </summary> /// <param name="inData">在操作过程中请勿对流进行关闭</param> /// <param name="propertyInfo">签章属性信息</param> /// <returns></returns> public override byte[] Sign(Stream inData, string propertyInfo) { //原文杂凑值计算 GeneralDigest digest = GetDigest(); byte[] block = inData.ToArray(); digest.BlockUpdate(block, 0, block.Length); byte[] outBytes = new byte[32]; digest.DoFinal(outBytes, 0); //计算杂凑值 DerUtcTime signTime = new DerUtcTime(DateTime.Now); TbsSign tbsSign = new TbsSign { Version = new DerInteger(1), EsSeal = _seal, TimeInfo = new DerBitString(signTime), DataHash = new DerBitString(outBytes), PropertyInfo = new DerIA5String(propertyInfo), Cert = new DerOctetString(_certificate.GetEncoded()), SignatureAlgorithm = GetSignAlgOId() }; ISigner signer = SignerUtilities.GetSigner(GMObjectIdentifiers.sm2sign_with_sm3); signer.Init(true, _privateKey); byte[] signBytes = tbsSign.GetDerEncoded(); signer.BlockUpdate(signBytes, 0, signBytes.Length); byte[] sign = signer.GenerateSignature(); SesSignature sesSignature = new SesSignature(tbsSign, new DerBitString(sign)); return(sesSignature.GetDerEncoded()); }
/// <summary> /// 对待签名数据签名 /// </summary> /// <param name="inData">在操作过程中请勿对流进行关闭</param> /// <param name="propertyInfo">签章属性信息</param> /// <returns></returns> public override byte[] Sign(Stream inData, string propertyInfo) { GeneralDigest md = GetDigest(); byte[] input = inData.ToArray(); md.BlockUpdate(input, 0, input.Length); byte[] output = new byte[32]; md.DoFinal(output, 0); TbsSign tbsSign = new TbsSign { Version = SesHeader.V4, EsSeal = _seal, TimeInfo = new DerGeneralizedTime(DateTime.Now), DataHash = new DerBitString(output), PropertyInfo = new DerIA5String(propertyInfo) }; ISigner signer = SignerUtilities.GetSigner(GMObjectIdentifiers.sm2sign_with_sm3); signer.Init(true, _privateKey); byte[] toSign = tbsSign.GetDerEncoded(); signer.BlockUpdate(toSign, 0, toSign.Length); byte[] signed = signer.GenerateSignature(); SesSignature sesSignature = new SesSignature(tbsSign, new DerOctetString(_certificate.GetEncoded()), GMObjectIdentifiers.sm2sign_with_sm3, new DerBitString(signed)); return(sesSignature.GetDerEncoded()); }
private static byte[] ComputeDigest(GeneralDigest digest, byte[] input) { digest.BlockUpdate(input, 0, input.Length); var output = new byte[digest.GetDigestSize()]; digest.DoFinal(output, 0); return(output); }
/// <summary> /// Hash email address using SHA-256 hashing algorithm. /// </summary> /// <param name="digest">Provides the algorithm for SHA-256.</param> /// <param name="email">The email address to hash.</param> /// <returns>Hash email address using SHA-256 hashing algorithm.</returns> private static String ToSha256String(GeneralDigest digest, String email) { byte[] data = Encoding.UTF8.GetBytes(email); byte[] digestBytes = new byte[digest.GetDigestSize()]; digest.BlockUpdate(data, 0, data.Length); digest.DoFinal(digestBytes, 0); return(BitConverter.ToString(digestBytes).Replace("-", string.Empty)); }
private string GetDigestValue(GeneralDigest digest) { var buffer = new byte[digest.GetDigestSize()]; digest.DoFinal(buffer, 0); return(Convert.ToBase64String(buffer)); }
static public byte[] Digest(byte[] data, GeneralDigest digest) { var hashed = new byte[digest.GetDigestSize()]; digest.BlockUpdate(data, 0, data.Length); digest.DoFinal(hashed, 0); return(hashed); }
/// <summary> /// Creates a human readable fingerprint for this certificate. This fingerprint may be /// compared by a user with an other certificate's fingerprint to proof their equality. /// </summary> protected static string CreateFingerprint(GeneralDigest a_digestGenerator, byte[] a_data) { var digestData = new byte[a_digestGenerator.GetDigestSize()]; a_digestGenerator.BlockUpdate(a_data, 0, a_data.Length); a_digestGenerator.DoFinal(digestData, 0); return(string.Join(":", digestData.Select(x => x.ToString("X2")))); }
/// <summary> /// Hash a string value using SHA-256 hashing algorithm. /// </summary> /// <param name="digest">Provides the algorithm for SHA-256.</param> /// <param name="value">The string value (e.g. an email address) to hash.</param> /// <returns>The hashed value.</returns> private static String ToSha256String(GeneralDigest digest, String value) { byte[] data = Encoding.UTF8.GetBytes(value); byte[] digestBytes = new byte[digest.GetDigestSize()]; digest.BlockUpdate(data, 0, data.Length); digest.DoFinal(digestBytes, 0); // Convert the byte array into an unhyphenated hexadecimal string. return(BitConverter.ToString(digestBytes).Replace("-", string.Empty)); }
private static byte[] HashIt(GeneralDigest hashAlgo, byte[] data, int offset = 0, int len = 0) { byte[] result; lock (hashAlgo) { hashAlgo.Reset(); hashAlgo.BlockUpdate(data, offset, len == 0 ? data.Length - offset : len); result = new byte[hashAlgo.GetDigestSize()]; hashAlgo.DoFinal(result, 0); } return(result); }