如何创建强大的 api 密钥

发布于 2024-12-11 08:29:57 字数 328 浏览 0 评论 0原文

我想创建一个安全的 REST API

例如,如果我看到 google API,则 API 密钥是根据域生成的。

由此我有两个问题:

第一,使用单向哈希是正确/正确的吗?如果是,如果有人知道哈希方法和域,那么他可以生成 api 密钥并使用它。我可以使用什么哈希方法/函数?

第二个是,如果客户制作一个桌面应用程序,他如何生成从桌面访问的 API KEY,现在是一个具有域 URL 的网站。我的意思是,他们可以生成 api 密钥,因为没有 url。

有什么好的方法吗?如何创建安全的 api 以及如何创建 api 密钥?

顺便说一句我正在使用 PHP

I want create a secure REST API

If i see the google API for example, the API KEY is generated based on domain.

from this i got two question :

First, is that true/right using one way hash ?, if yes how if someone know the hash method and the domain, so he can generate api key and use it. and what hash method/function that i can use ?

The second is, how if client make a desktop application, how he can generate API KEY which accessed from desktop, now a website that have a domain url. i mean, they can generate api key because not have url.

is there any good way ?, how to create a secured api and how to create a api key ?

btw i'm using PHP

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

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

发布评论

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

评论(4

铁憨憨 2024-12-18 08:29:57
  1. 您可以使用多种哈希方法并将其与附加盐混合使用,这些方法可以基于域+其他逻辑,这将非常难以猜测或破解(取决于您将使用什么哈希算法以及其他事情),除非有人知道它是如何完成的。
    您还可以生成 UUID 并将其用作 api 密钥(可能我会使用它),http://en。例如 wikipedia.org/wiki/Uuid 版本 4,您可以检查它的实现细节,甚至可以轻松地想到一些改进。

  2. 不太确定您的意思。

  1. you can use and mix many hash methods with additional salt, which can be based on domain+other logic, that will be very difficult to guess or crack (depends what hash algorithms you will use and other things), unless someone knows how its done.
    you can also generate UUID and use it as api key(probably i would use that), http://en.wikipedia.org/wiki/Uuid version 4 for example, you can check the implementation details of it, or even think of some improvements easily.

  2. not quite sure what you mean there.

海未深 2024-12-18 08:29:57

如果您打算使用单向哈希,那么您绝对应该考虑加盐。您可以生成类似 10 个字符的随机字符串(这将是您的盐),将盐附加或前置到域(您最初想要散列的内容),然后将其传递给您选择的单向散列算法。

我猜你正在将 API 密钥存储在某种数据库中。您首先要执行我上面解释的操作,然后将经过加盐和哈希处理的密码与您生成的随机盐一起存储在数据库中。这样,如果有人知道您正在使用的哈希算法,他们仍然需要获得盐。如果你让你的盐随机(就像我之前说的),他们很可能无法猜到:)

如果你打算使用这种方法,还有一个额外的步骤。检查 API 密钥是否正确时,您将获取给定的 API 密钥,浏览表并找到您在该 API 密钥上使用的盐(您可以使用用户用户名查询表),将该盐附加或添加到密钥,然后通过相同的哈希算法发送它。如果它与您数据库中的密钥匹配,那么它就是正确的密钥!

希望这有帮助! :)

If you plan on using a one way hash, you should definitely look into salting. You can generate something like a 10 character random string (this will be your salt), either append or prepend the salt to domain (what you initially wanted to hash), then pass it through a one way hashing algorithm of your choice.

I'm guessing you're storing your API keys inside some sort of database. You would first do what I explained above, then store that salted and hashed password in your database along with the random salt you generated. This way if someone knows the hashing algorithm you're using, they would still need to get their hands on the salt. If you make your salt random (like i said earlier), they most likely won't be able to guess it :)

There's one additional step if you plan on using this approach. When checking if the API key is correct, you would take the given API key, go through your table and find the salt you used on that API key (you can query the table using the users username), append or prepend that salt to the key, then send it through the same hashing algorithm. If it matches the one in your database, it's a correct key!

Hope this helps! :)

岁月流歌 2024-12-18 08:29:57

好的,如果您有一个存储 API 密钥和使用它们的客户端的数据库表,您将为所有客户端构建唯一的密钥。您可以轻松地随机化字符,也可以选择对它们进行散列并将其存储为密钥。

例如。

$length = 16; // 16 Chars long
$key = "";
for ($i=1;$i<=$length;$i++) {
  // Alphabetical range
  $alph_from = 65;
  $alph_to = 90;

  // Numeric
  $num_from = 48;
  $num_to = 57;

  // Add a random num/alpha character
  $chr = rand(0,1)?(chr(rand($alph_from,$alph_to))):(chr(rand($num_from,$num_to)));
  if (rand(0,1)) $chr = strtolower($chr);
  $key.=$chr;
  }

如果你想对此进行散列,可以使用 MD5 或 SHA1,但你需要在数据库中进行反向比较,例如。

SELECT * FROM api_keys where MD5(key) = INPUT

我希望这有帮助

Ok, so if you have a database table storing API keys and clients using them, you would build unique keys for all clients. You can easily randomize characters, optionally hash them and store that as the key.

eg.

$length = 16; // 16 Chars long
$key = "";
for ($i=1;$i<=$length;$i++) {
  // Alphabetical range
  $alph_from = 65;
  $alph_to = 90;

  // Numeric
  $num_from = 48;
  $num_to = 57;

  // Add a random num/alpha character
  $chr = rand(0,1)?(chr(rand($alph_from,$alph_to))):(chr(rand($num_from,$num_to)));
  if (rand(0,1)) $chr = strtolower($chr);
  $key.=$chr;
  }

If you want to hash this, you can use MD5 or SHA1 but you will need to reverse compare in the database, eg.

SELECT * FROM api_keys where MD5(key) = INPUT

I hope this helps

凡尘雨 2024-12-18 08:29:57

该函数每次都会返回随机字符串。这将帮助您通过调用两次来生成 API Key 和 Secret,并将值存储到变量中。这是每次生成新密钥的简单方法。

function generateRandomString($length = 30)
{
$characters='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0987654321'.time();
$charactersLength = strlen($characters);
$randomString = '';
  for ($i = $length; $i > 0; $i--)
  {
    $randomString .= $characters[rand(0, $charactersLength - 1)];
  }
  return $randomString;
}


I think this is simple and usefully.

This Function will return Random string every time. This will help you to generate API Key and Secret by calling two times and store value into the variable.This is the simple way to generate every time new key..

function generateRandomString($length = 30)
{
$characters='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0987654321'.time();
$charactersLength = strlen($characters);
$randomString = '';
  for ($i = $length; $i > 0; $i--)
  {
    $randomString .= $characters[rand(0, $charactersLength - 1)];
  }
  return $randomString;
}


I think this is simple and usefully.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文