gpt4 book ai didi

c# - 只允许授权代码调用库中的例程

转载 作者:行者123 更新时间:2023-11-30 03:22:17 25 4
gpt4 key购买 nike

好的,我将在这里尝试解释我的使用场景:我有一个核心库的实现,其中最低级别的访问是将数据输入数据库的 API,它应该作为一个子系统自包含。现在,由于奇怪的原因,这个库向这个数据库公开了关键参数,如登录 ID、密码等。它旨在仅用于同一团队开发的一些更大应用程序的“内部”代码,其想法是不鼓励其他可以访问二进制相同库的第三方客户端使用这些 API。这是它变得更丑陋的部分。为了实现这些相当“ secret ”的功能,设计了一个先前确定的 secret key 的简单实现。比如说,这样的代码:

string secretKey = "blah...$$$";
string password = library::secretGetPwd(secretKey);

这在多个地方使用,并且该功能仅在 secretKey 正确时才有效。这似乎不是一个好方法,很容易被发现。然而,它确实在几个地方引起了几个魔术字符串的警告,这使得 lint 工具非常不愉快。手头的任务是只删除这些 lint 警告。有人可以建议一些“更好”更糟糕的方法来实现这个吗?

最佳答案

不回答你的 lint 问题,但你可以通过这种方式解决更大的问题:

首先,强烈签署你所有的东西。然后您可以查看调用堆栈以查看调用代码的程序集,并验证它是您的(代码使用公钥来验证它是您的[可能不是一个好方法])。这是 100% 安全的吗?我对此表示怀疑。

    //This code relies on the public keys of the two assemblies to be the same.
System::Diagnostics::StackTrace^ st = gcnew System::Diagnostics::StackTrace();
System::Reflection::Assembly ^ otherAsm = st->GetFrame(1)->GetMethod()->Module->Assembly;
array<unsigned char, 1> ^ otherKey = otherAsm->GetName()->GetPublicKey();

System::Reflection::Assembly ^ thisAsm = st->GetFrame(0)->GetMethod()->Module->Assembly;
array<unsigned char, 1> ^ thisKey = thisAsm->GetName()->GetPublicKey();

for (int i = 0; i < thisKey->Length; i++)
{
if (otherKey[i] != thisKey[i])
throw gcnew Exception();
}

关于c# - 只允许授权代码调用库中的例程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51277811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com