The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

87 lines
2.9 KiB

using Org.BouncyCastle.Bcpg.OpenPgp;
using Org.BouncyCastle.Utilities.Encoders;
using System;
using System.IO;
using System.Text;
namespace Teknik.Utilities.Cryptography
{
public static class PGP
{
public static bool IsPublicKey(string key)
{
bool isValid = false;
try
{
byte[] byteArray = Encoding.ASCII.GetBytes(key);
using (MemoryStream stream = new MemoryStream(byteArray))
{
using (Stream decoderStream = PgpUtilities.GetDecoderStream(stream))
{
PgpPublicKeyRingBundle publicKeyBundle = new PgpPublicKeyRingBundle(decoderStream);
PgpPublicKey foundKey = GetFirstPublicKey(publicKeyBundle);
if (foundKey != null)
{
isValid = true;
}
}
}
}
catch (Exception)
{
isValid = false;
}
return isValid;
}
public static string GetFingerprint(string key)
{
string hexString = string.Empty;
byte[] byteArray = Encoding.ASCII.GetBytes(key);
using (MemoryStream stream = new MemoryStream(byteArray))
{
using (Stream decoderStream = PgpUtilities.GetDecoderStream(stream))
{
PgpPublicKeyRingBundle publicKeyBundle = new PgpPublicKeyRingBundle(decoderStream);
PgpPublicKey foundKey = GetFirstPublicKey(publicKeyBundle);
if (foundKey != null)
{
byte[] fing = foundKey.GetFingerprint();
hexString = Hex.ToHexString(fing);
}
}
}
return hexString;
}
public static string GetFingerprint64(string key)
{
string fingerprint = GetFingerprint(key);
if (fingerprint.Length > 16)
fingerprint = fingerprint.Substring(fingerprint.Length - 16);
return fingerprint;
}
private static PgpPublicKey GetFirstPublicKey(PgpPublicKeyRingBundle publicKeyRingBundle)
{
foreach (PgpPublicKeyRing kRing in publicKeyRingBundle.GetKeyRings())
{
var keys = kRing.GetPublicKeys();
foreach (var key in keys)
{
PgpPublicKey foundKey = (PgpPublicKey)key;
//PgpPublicKey key = kRing.GetPublicKeys()
//.Cast<PgpPublicKey>()
// .Where(k => k.IsEncryptionKey)
// .FirstOrDefault();
if (foundKey != null && foundKey.IsEncryptionKey)
return foundKey;
}
}
return null;
}
}
}