public override byte[] Sign(byte[] msg) { SHA1Digest hash = new SHA1Digest(); hash.update(msg, 0, msg.Length); byte[] data = new byte[hash.getDigestSize()]; hash.doFinal(data, 0); byte[] tmp = new byte[data.Length + ASN_SHA1.Length]; Array.Copy(ASN_SHA1, 0, tmp, 0, ASN_SHA1.Length); Array.Copy(data, 0, tmp, ASN_SHA1.Length, data.Length); data = tmp; BigInteger dataInt = new BigInteger(1, data); int mLen = (Modulus.bitLength() + 7) / 8; dataInt = RSA.padPKCS1(dataInt, 1, mLen, new RNGCryptoServiceProvider()); BigInteger signatureInt = null; signatureInt = RSA.doPrivateCrt(dataInt, PrimeP, PrimeQ, PrimeExponentP, PrimeExponentQ, CrtCoefficient); byte[] sig = unsignedBigIntToBytes(signatureInt, mLen); return(sig); }
protected internal bool VerifySignature(byte[] msg, BigInteger r, BigInteger s) { // Create a SHA1 hash of the message SHA1Digest h = new SHA1Digest(); h.update(msg, 0, msg.Length); byte[] data = new byte[h.getDigestSize()]; h.doFinal(data, 0); BigInteger m = new BigInteger(1, data); m = m.mod(q); if (BigInteger.valueOf(0).compareTo(r) >= 0 || q.compareTo(r) <= 0) { return(false); } if (BigInteger.valueOf(0).compareTo(s) >= 0 || q.compareTo(s) <= 0) { return(false); } BigInteger w = s.modInverse(q); BigInteger u1 = m.multiply(w).mod(q); BigInteger u2 = r.multiply(w).mod(q); BigInteger v = g.modPow(u1, p).multiply(y.modPow(u2, p)).mod(p).mod(q); return(v.compareTo(r) == 0); }
public void NoInput_CorrectOutput() { var digest = new SHA1Digest(); var result = digest.DigestBuffer(); AssertSHA1("da39a3ee5e6b4b0d3255bfef95601890afd80709", result); }
public byte[] Sign(byte[] msg) { SHA1Digest h = new SHA1Digest(); h.update(msg, 0, msg.Length); byte[] data = new byte[h.getDigestSize()]; h.doFinal(data, 0); return(DSA.Sign(x, p, q, g, data)); }
public virtual bool VerifySignature(byte[] signature, byte[] msg) { // Create a SHA1 hash of the message SHA1Digest h = new SHA1Digest(); h.update(msg, 0, msg.Length); byte[] data = new byte[h.getDigestSize()]; h.doFinal(data, 0); return(DSA.Verify(y, p, q, g, signature, data)); }
/** * * Calulates the keyidentifier using a SHA1 hash over the BIT STRING * from SubjectPublicKeyInfo as defined in RFC2459. * * Example of making a AuthorityKeyIdentifier: * <pre> * SubjectPublicKeyInfo apki = new SubjectPublicKeyInfo((ASN1Sequence)new DERInputStream( * new ByteArrayInputStream(publicKey.getEncoded())).readObject()); * AuthorityKeyIdentifier aki = new AuthorityKeyIdentifier(apki); * </pre> * **/ public AuthorityKeyIdentifier( SubjectPublicKeyInfo spki) { Digest digest = new SHA1Digest(); byte[] resBuf = new byte[digest.getDigestSize()]; byte[] bytes = spki.getPublicKeyData().getBytes(); digest.update(bytes, 0, bytes.Length); digest.doFinal(resBuf, 0); this.keyidentifier = new DEROctetString(resBuf); }
public void StringInput_CorrectOutput() { var digest = new SHA1Digest(); var buffer = Encoding.UTF8.GetBytes("The quick brown fox jumps over the lazy dog"); digest.Update(buffer); var result = digest.DigestBuffer(); AssertSHA1("2fd4e1c67a2d28fced849ee1bb76e7391b93eb12", result); }
public void SimpleString_CorrectOutput() { var digest = new SHA1Digest(); var buffer = new byte[] { 0x24 }; digest.Update(buffer); var result = digest.DigestBuffer(); AssertSHA1("3cdf2936da2fc556bfa533ab1eb59ce710ac80e5", result); }
/// <summary> /// Create using an public Asymmetric Key. /// </summary> /// <param name="key">A public Asymmetric key.</param> public RespID(AsymmetricKeyParameter key) { SubjectPublicKeyInfo info = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(key); byte[] b = info.getEncoded(); SHA1Digest sha1 = new SHA1Digest(); sha1.update(b, 0, b.Length); b = new byte[sha1.getDigestSize()]; sha1.doFinal(b, 0); ASN1OctetString keyHash = new DEROctetString(b); this.id = new ResponderID(keyHash); }
/** * create an AuthorityKeyIdentifier with the GeneralNames tag and * the serial number provided as well. */ public AuthorityKeyIdentifier( SubjectPublicKeyInfo spki, GeneralNames name, BigInteger serialNumber) { Digest digest = new SHA1Digest(); byte[] resBuf = new byte[digest.getDigestSize()]; byte[] bytes = spki.getPublicKeyData().getBytes(); digest.update(bytes, 0, bytes.Length); digest.doFinal(resBuf, 0); this.keyidentifier = new DEROctetString(resBuf); this.certissuer = name; this.certserno = new DERInteger(serialNumber); }
private static void RunNIST(string file) { var lines = File.ReadAllLines("TestVectors/" + file); for (var i = 0; i < lines.Length; i += 4) { var digest = new SHA1Digest(); var len = int.Parse(lines[i].Substring(6)) / 8; var msg = HexConverter.FromHex(lines[i + 1].Substring(6)); var expectedHash = lines[i + 2].Substring(5); digest.Update(msg.AsSpan(0, len)); var hash = digest.DigestBuffer(); AssertSHA1(expectedHash, hash); } }
public void Clone_SeperateStateFromOriginal() { var digest = new SHA1Digest(); var buffer = Encoding.UTF8.GetBytes("The quick brown fox jumps"); digest.Update(buffer); var digest2 = digest.Clone(); buffer = Encoding.UTF8.GetBytes(" over the lazy dog"); digest2.Update(buffer); var result1 = digest.DigestBuffer(); var result2 = digest2.DigestBuffer(); AssertSHA1("743e27565bb39d4cf6cdf7b19450f94ef12b2206", result1); AssertSHA1("2fd4e1c67a2d28fced849ee1bb76e7391b93eb12", result2); }
public virtual bool VerifySignature(byte[] signature, byte[] msg) { BigInteger signatureInt = new BigInteger(signature); signatureInt = RSA.doPublic(signatureInt, modulus, publicExponent); signatureInt = RSA.removePKCS1(signatureInt, 1); signature = signatureInt.toByteArray(); SHA1Digest h = new SHA1Digest(); h.update(msg, 0, msg.Length); byte[] data = new byte[h.getDigestSize()]; h.doFinal(data, 0); if (data.Length != (signature.Length - ASN_SHA1.Length)) { return(false); } byte[] cmp = ASN_SHA1; for (int i = 0, j = 0; i < signature.Length; i++, j++) { if (i == ASN_SHA1.Length) { cmp = data; j = 0; } if (signature[i] != cmp[j]) { return(false); } } return(true); }
/** * which generates the p and g values from the given parameters, * returning the DSAParameters object. * <p> * Note: can take a while... */ public DSAParameters generateParameters() { byte[] seed = new byte[20]; byte[] part1 = new byte[20]; byte[] part2 = new byte[20]; byte[] u = new byte[20]; SHA1Digest sha1 = new SHA1Digest(); int n = (size - 1) / 160; byte[] w = new byte[size / 8]; BigInteger q = null, p = null, g = null; int counter = 0; bool primesFound = false; while (!primesFound) { do { random.nextBytes(seed); sha1.update(seed, 0, seed.Length); sha1.doFinal(part1, 0); Array.Copy(seed, 0, part2, 0, seed.Length); add(part2, seed, 1); sha1.update(part2, 0, part2.Length); sha1.doFinal(part2, 0); for (int i = 0; i != u.Length; i++) { u[i] = (byte)(part1[i] ^ part2[i]); } u[0] |= (byte)0x80; u[19] |= (byte)0x01; q = new BigInteger(1, u); }while (!q.isProbablePrime(certainty)); counter = 0; int offset = 2; while (counter < 4096) { for (int k = 0; k < n; k++) { add(part1, seed, offset + k); sha1.update(part1, 0, part1.Length); sha1.doFinal(part1, 0); Array.Copy(part1, 0, w, w.Length - (k + 1) * part1.Length, part1.Length); } add(part1, seed, offset + n); sha1.update(part1, 0, part1.Length); sha1.doFinal(part1, 0); Array.Copy(part1, part1.Length - ((w.Length - (n) * part1.Length)), w, 0, w.Length - n * part1.Length); w[0] |= (byte)0x80; BigInteger x = new BigInteger(1, w); BigInteger c = x.mod(q.multiply(TWO)); p = x.subtract(c.subtract(ONE)); if (p.testBit(size - 1)) { if (p.isProbablePrime(certainty)) { primesFound = true; break; } } counter += 1; offset += n + 1; } } // // calculate the generator g // BigInteger pMinusOneOverQ = p.subtract(ONE).divide(q); for (;;) { BigInteger h = new BigInteger(size, random); if (h.compareTo(ONE) <= 0 || h.compareTo(p.subtract(ONE)) >= 0) { continue; } g = h.modPow(pMinusOneOverQ, p); if (g.compareTo(ONE) <= 0) { continue; } break; } return(new DSAParameters(p, q, g, new DSAValidationParameters(seed, counter))); }