什么是提供 API 服务且不会产生大量通信开销的安全方法?
几天前我参加了一次面试,雇主问我如何安全地提供 API 服务。他说
- 每个 API 请求都需要 API 密钥。
- 每个 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
- Each API request requires API key.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将请求与 API 密钥一起进行哈希处理。然后将哈希添加到请求中。
这样,哈希值就不会通过网络传输。服务器可以通过自己的 API 密钥副本进行验证。
例如,客户端执行:
并且服务器执行:
请注意,这仍然面临至少两个问题:
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:
And the server does:
Note that this still suffers from at least two problems: