public byte[] CalculateFingerprint(PublicKeyPacket publicPk) { IBcpgKey key = publicPk.Key; if (publicPk.Version <= 3) { RsaPublicBcpgKey rK = (RsaPublicBcpgKey)key; try { // TODO: MD5 needs to go in the main API... MD5Digest digest = new MD5Digest(); byte[] bytes = new MPInteger(rK.Modulus).GetEncoded(); digest.BlockUpdate(bytes, 2, bytes.Length - 2); bytes = new MPInteger(rK.PublicExponent).GetEncoded(); digest.BlockUpdate(bytes, 2, bytes.Length - 2); byte[] digBuf = new byte[digest.GetDigestSize()]; digest.DoFinal(digBuf, 0); return(digBuf); } catch (IOException e) { throw new PgpException("can't encode key components: " + e.Message, e); } } else { try { byte[] kBytes = publicPk.GetEncodedContents(); IStreamCalculator <IBlockResult> hashCalc = CryptoServicesRegistrar.CreateService(FipsShs.Sha1).CreateCalculator(); Stream hStream = hashCalc.Stream; hStream.WriteByte((byte)0x99); hStream.WriteByte((byte)(kBytes.Length >> 8)); hStream.WriteByte((byte)kBytes.Length); hStream.Write(kBytes, 0, kBytes.Length); hStream.Close(); return(hashCalc.GetResult().Collect()); } catch (IOException e) { throw new PgpException("can't encode key components: " + e.Message, e); } } }
public static byte[] CalculateFingerprint(PublicKeyPacket publicPk) { IBcpgKey key = publicPk.Key; IDigest digest; if (publicPk.Version <= 3) { RsaPublicBcpgKey rK = (RsaPublicBcpgKey)key; try { digest = DigestUtilities.GetDigest("MD5"); UpdateDigest(digest, rK.Modulus); UpdateDigest(digest, rK.PublicExponent); } catch (Exception e) { throw new PgpException("can't encode key components: " + e.Message, e); } } else { try { byte[] kBytes = publicPk.GetEncodedContents(); digest = DigestUtilities.GetDigest("SHA1"); digest.Update(0x99); digest.Update((byte)(kBytes.Length >> 8)); digest.Update((byte)kBytes.Length); digest.BlockUpdate(kBytes, 0, kBytes.Length); } catch (Exception e) { throw new PgpException("can't encode key components: " + e.Message, e); } } return(DigestUtilities.DoFinal(digest)); }
public static byte[] CalculateFingerprint(PublicKeyPacket publicPk) { IBcpgKey key = publicPk.Key; IDigest digest; if (publicPk.Version <= 3) { RsaPublicBcpgKey rK = (RsaPublicBcpgKey)key; try { digest = DigestUtilities.GetDigest("MD5"); UpdateDigest(digest, rK.Modulus); UpdateDigest(digest, rK.PublicExponent); } catch (Exception e) { throw new PgpException("can't encode key components: " + e.Message, e); } } else { try { byte[] kBytes = publicPk.GetEncodedContents(); digest = DigestUtilities.GetDigest("SHA1"); digest.Update(0x99); digest.Update((byte)(kBytes.Length >> 8)); digest.Update((byte)kBytes.Length); digest.BlockUpdate(kBytes, 0, kBytes.Length); } catch (Exception e) { throw new PgpException("can't encode key components: " + e.Message, e); } } return DigestUtilities.DoFinal(digest); }
public byte[] GetEncodedContents() { MemoryStream memoryStream = new MemoryStream(); BcpgOutputStream bcpgOutputStream = new BcpgOutputStream(memoryStream); bcpgOutputStream.Write(pubKeyPacket.GetEncodedContents()); bcpgOutputStream.WriteByte((byte)s2kUsage); if (s2kUsage == 255 || s2kUsage == 254) { bcpgOutputStream.WriteByte((byte)encAlgorithm); bcpgOutputStream.WriteObject(s2k); } if (iv != null) { bcpgOutputStream.Write(iv); } if (secKeyData != null && secKeyData.Length > 0) { bcpgOutputStream.Write(secKeyData); } return(memoryStream.ToArray()); }
public static byte[] CalculateFingerprint(PublicKeyPacket publicPk) { IBcpgKey key = publicPk.Key; IDigest digest; if (publicPk.Version <= 3) { RsaPublicBcpgKey rsaPublicBcpgKey = (RsaPublicBcpgKey)key; try { digest = DigestUtilities.GetDigest("MD5"); UpdateDigest(digest, rsaPublicBcpgKey.Modulus); UpdateDigest(digest, rsaPublicBcpgKey.PublicExponent); } catch (global::System.Exception ex) { throw new PgpException("can't encode key components: " + ex.get_Message(), ex); } } else { try { byte[] encodedContents = publicPk.GetEncodedContents(); digest = DigestUtilities.GetDigest("SHA1"); digest.Update(153); digest.Update((byte)(encodedContents.Length >> 8)); digest.Update((byte)encodedContents.Length); digest.BlockUpdate(encodedContents, 0, encodedContents.Length); } catch (global::System.Exception ex2) { throw new PgpException("can't encode key components: " + ex2.get_Message(), ex2); } } return(DigestUtilities.DoFinal(digest)); }
private void Init() { IBcpgKey key = publicPk.Key; if (publicPk.Version <= 3) { RsaPublicBcpgKey rK = (RsaPublicBcpgKey)key; this.keyId = rK.Modulus.LongValue; try { IDigest digest = DigestUtilities.GetDigest("MD5"); byte[] bytes = rK.Modulus.ToByteArrayUnsigned(); digest.BlockUpdate(bytes, 0, bytes.Length); bytes = rK.PublicExponent.ToByteArrayUnsigned(); digest.BlockUpdate(bytes, 0, bytes.Length); this.fingerprint = DigestUtilities.DoFinal(digest); } //catch (NoSuchAlgorithmException) catch (Exception e) { throw new IOException("can't find MD5", e); } this.keyStrength = rK.Modulus.BitLength; } else { byte[] kBytes = publicPk.GetEncodedContents(); try { IDigest digest = DigestUtilities.GetDigest("SHA1"); digest.Update(0x99); digest.Update((byte)(kBytes.Length >> 8)); digest.Update((byte)kBytes.Length); digest.BlockUpdate(kBytes, 0, kBytes.Length); this.fingerprint = DigestUtilities.DoFinal(digest); } catch (Exception e) { throw new IOException("can't find SHA1", e); } this.keyId = (long)(((ulong)fingerprint[fingerprint.Length - 8] << 56) | ((ulong)fingerprint[fingerprint.Length - 7] << 48) | ((ulong)fingerprint[fingerprint.Length - 6] << 40) | ((ulong)fingerprint[fingerprint.Length - 5] << 32) | ((ulong)fingerprint[fingerprint.Length - 4] << 24) | ((ulong)fingerprint[fingerprint.Length - 3] << 16) | ((ulong)fingerprint[fingerprint.Length - 2] << 8) | (ulong)fingerprint[fingerprint.Length - 1]); if (key is RsaPublicBcpgKey) { this.keyStrength = ((RsaPublicBcpgKey)key).Modulus.BitLength; } else if (key is DsaPublicBcpgKey) { this.keyStrength = ((DsaPublicBcpgKey)key).P.BitLength; } else if (key is ElGamalPublicBcpgKey) { this.keyStrength = ((ElGamalPublicBcpgKey)key).P.BitLength; } } }