什么是提供 API 服务且不会产生大量通信开销的安全方法?

发布于 2025-01-08 03:31:38 字数 437 浏览 1 评论 0原文

几天前我参加了一次面试,雇主问我如何安全地提供 API 服务。他说

  1. 每个 API 请求都需要 API 密钥。
  2. 每个 API 请求将请求 1 个单一方法,该方法采用单一参数,并且每个服务都将通过该单一方法进行工作。

他想看看是否有一种安全的方法可以让客户端在不暴露 API 密钥的情况下向服务器端发出 API 请求。

我谈到了通用公共/私有密码学,但他想要比这更简单的方法。

最后,他谈到使用常见的哈希算法(md5,sha1)让客户端安全地哈希其密钥,并哈希带有参数和向服务器请求的方法,但我认为我没有很好地理解这一点。

我记得有些库首先对 API 请求的主体进行编码,然后使用 md5 或 sha1 进行加密。但是用单向哈希来做这件事有什么意义呢?中间人可能不知道API密钥,但是服务器如何知道1.API密钥,2.客户端请求了什么方法?

I had an interview few days ago, and the employer asked me about secure way to serve API service. He said

  1. Each API request requires API key.
  2. Each API request will request 1 single method, which takes a single parameter, and every service will work through this single method.

He wanted to see if there is a secure way for a client side to API request to the server side without exposing the API key.

I talked about general public/private cryptography, but he wanted simpler method than that.

In the end, he talked about using common hash algorithm (md5, sha1) for client side to safely hash its key, and also hash the method with parameter and request to the server, but I do not think I got the point very well.

I remember some libraries out there first encodes the body of the API request to be encrypted with md5 or sha1. But what is the point of doing it with 1-way hash? It may be true that man-in-the-middle won't know about API key, but how will server know 1.API key, 2.What method that client has requested?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千仐 2025-01-15 03:31:38

您可以将请求与 API 密钥一起进行哈希处理。然后将哈希添加到请求中。

这样,哈希值就不会通过网络传输。服务器可以通过自己的 API 密钥副本进行验证。


例如,客户端执行:

var request = "http://example.com/bla?a=1&b=2";
var hash = sha1(request + ApiKey).ToHex();
var request-with-hash = request +"&key="+hash;

并且服务器执行:

var receivedRequest = "http://example.com/bla?a=1&b=2&key=ABC...09"
var key=extractKey(receivedRequest); // ABC...09
var strippedRequest = stripKeyFromRequest(receivedRequest);
var hash = sha1(strippedRequest + ApiKey).ToHex();
if(hash!=key)
  Error("ApiKeyWrong")

请注意,这仍然面临至少两个问题:

  1. 重放攻击。通过使用更好的协议和某种随机数可以避免这种情况。
  2. API 密钥可以简单地从客户端提取,除非它在安全系统上运行。这是一个根本性的问题,无法回避。不受信任的系统上的 ApiKey 并不安全。

You can hash the request together with API key. And then add the hash to the request.

That way the hash never goes over the wire. And the server can verify from his own copy of the API key.


For example the client does:

var request = "http://example.com/bla?a=1&b=2";
var hash = sha1(request + ApiKey).ToHex();
var request-with-hash = request +"&key="+hash;

And the server does:

var receivedRequest = "http://example.com/bla?a=1&b=2&key=ABC...09"
var key=extractKey(receivedRequest); // ABC...09
var strippedRequest = stripKeyFromRequest(receivedRequest);
var hash = sha1(strippedRequest + ApiKey).ToHex();
if(hash!=key)
  Error("ApiKeyWrong")

Note that this still suffers from at least two problems:

  1. Replay attacks. This can be avoided by using a better protocol, with some kind of nonce.
  2. The API key can be simply extract from the client, unless it's running on a secure system. This is a fundamental problem, and cannot be avoided. ApiKeys on untrusted systems are not secure.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文