public static byte[] ExportPubKey(tbl_PublicKey key, SshPublicKeyFormat pubKeyFormat) { var pubBytes = Encoding.ASCII.GetBytes(key.KeyValue); var pubKeyInfo = new PublicKeyInfo(); pubKeyInfo.Load(new MemoryStream(pubBytes)); var pubStream = new MemoryStream(); var pubKey = new SshPublicKey(pubKeyInfo); pubKey.SavePublicKey(pubStream, pubKeyFormat); return(pubStream.ToArray()); }
public static tbl_PublicKey ImportPubKey(IUnitOfWork uow, tbl_User user, SignatureHashAlgorithm sigAlgo, string hostname, FileInfo inputFile) { var callPath = $"{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}"; tbl_PublicKey pubKeyEntity = null; var pubKey = new SshPublicKey(inputFile.FullName); var pubKeyStream = new MemoryStream(); pubKey.SavePublicKey(pubKeyStream, SshPublicKeyFormat.Pkcs8); var pubKeyValue = Encoding.ASCII.GetString(pubKeyStream.ToArray()); var pubKeyFound = uow.PublicKeys.Get(QueryExpressionFactory.GetQueryExpression <tbl_PublicKey>() .Where(x => x.Id == user.IdentityId && x.KeyValue == pubKeyValue).ToLambda()); if (pubKeyFound == null) { pubKeyEntity = uow.PublicKeys.Create( new tbl_PublicKey { Id = Guid.NewGuid(), IdentityId = user.IdentityId, KeyValue = pubKeyValue, KeyAlgo = pubKey.KeyAlgorithm.ToString(), KeyFormat = SshPublicKeyFormat.Pkcs8.ToString(), SigValue = pubKey.Fingerprint.ToString(sigAlgo, false), SigAlgo = sigAlgo.ToString(), Comment = hostname, Enabled = true, Deletable = true, Created = DateTime.Now, LastUpdated = null, }); Log.Information($"'{callPath}' '{user.IdentityAlias}' public key algo {pubKey.KeyAlgorithm} sig {pubKey.Fingerprint.ToString(sigAlgo, false)}" + $"{Environment.NewLine}{pubKeyValue}"); } else { Log.Warning($"'{callPath}' '{user.IdentityAlias}' skip public key algo {pubKey.KeyAlgorithm} sig {pubKey.Fingerprint.ToString(sigAlgo, false)}" + $"{Environment.NewLine}{pubKeyValue}"); } uow.Commit(); return(pubKeyEntity); }