【问题标题】:is base64 encoding url safe?base64编码网址安全吗?
【发布时间】:2012-09-27 22:32:37
【问题描述】:

我正在使用 node.bcrypt.js 哈希返回 node.js 中的十六进制数字作为密码重置令牌。

user.reset_password_token = require('crypto').randomBytes(32).toString('hex'
);

在将令牌传递到 url 之前,我是否也应该对令牌进行 base64 编码(即:链接重置电子邮件)?

这样做有什么好处吗?

我似乎记得 base64 编码可能包含会弄乱路径的正斜杠:

   var token = user.reset_password_token;

   //is there any benefit to doing base64 encoding?
   var encoded_token = new Buffer(token).toString('base64');

   var reset_link = 'http://example.com/reset/'+ encoded_token;
   sendResetLink( reset_link );

【问题讨论】:

标签: node.js url base64


【解决方案1】:

我在https://www.npmjs.org/package/urlsafe-base64 使用 URLSafeBase64 nodejs LIB 解决了这个问题

var email =email_lines.join("\r\n").trim();
var base64EncodedEmail = URLSafeBase64.encode(new Buffer(email));
gmail.users.messages.send({userId:"me",
        resource: {raw:base64EncodedEmail} }, callbackFn});

【讨论】:

    【解决方案2】:

    您不需要第三方库。您可以只使用base64url 编码(从nodejs v14 开始)

    const encoded_token = Buffer.from(token).toString('base64url');
    

    【讨论】:

      【解决方案3】:

      base64 确实可以包含正斜杠,但base32 不能!

      【讨论】:

      • 这是否暗示它 base64 不适合 URI(例如,它们是否 无效)?还是只是处理方式需要不同?
      • 查看可能的字符集。如果您不更改用于编码的可用字符,则单元格可以(理论上)等于 64 的值,这将在您的字符串中为您提供 /。更改 base64 函数的字符集或使用 base32
      • 谢谢,您认为使用 base32 编码的字符串而不是未编码的令牌有什么好处吗?
      • base32 编码的字符串将占用比 base64 更多的空间,因为只有一半的字符可以编码,我认为没有任何好处,没有 :)
      【解决方案4】:

      另一个选项是base64url library:

      base64url("ladies and gentlemen we are floating in space");
      // bGFkaWVzIGFuZCBnZW50bGVtZW4gd2UgYXJlIGZsb2F0aW5nIGluIHNwYWNl
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-08-06
        • 2020-08-29
        • 1970-01-01
        • 2014-12-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多