<i id='qCCUs'><tr id='qCCUs'><dt id='qCCUs'><q id='qCCUs'><span id='qCCUs'><b id='qCCUs'><form id='qCCUs'><ins id='qCCUs'></ins><ul id='qCCUs'></ul><sub id='qCCUs'></sub></form><legend id='qCCUs'></legend><bdo id='qCCUs'><pre id='qCCUs'><center id='qCCUs'></center></pre></bdo></b><th id='qCCUs'></th></span></q></dt></tr></i><div id='qCCUs'><tfoot id='qCCUs'></tfoot><dl id='qCCUs'><fieldset id='qCCUs'></fieldset></dl></div>

    <small id='qCCUs'></small><noframes id='qCCUs'>

    <tfoot id='qCCUs'></tfoot>

        <bdo id='qCCUs'></bdo><ul id='qCCUs'></ul>
    1. <legend id='qCCUs'><style id='qCCUs'><dir id='qCCUs'><q id='qCCUs'></q></dir></style></legend>
      1. 在 JavaScript 中使用两种方式散列 JSON 字符串以用于 URL

        时间:2023-09-30

            • <bdo id='AHFZO'></bdo><ul id='AHFZO'></ul>

              <tfoot id='AHFZO'></tfoot>

              <small id='AHFZO'></small><noframes id='AHFZO'>

                <tbody id='AHFZO'></tbody>

              1. <i id='AHFZO'><tr id='AHFZO'><dt id='AHFZO'><q id='AHFZO'><span id='AHFZO'><b id='AHFZO'><form id='AHFZO'><ins id='AHFZO'></ins><ul id='AHFZO'></ul><sub id='AHFZO'></sub></form><legend id='AHFZO'></legend><bdo id='AHFZO'><pre id='AHFZO'><center id='AHFZO'></center></pre></bdo></b><th id='AHFZO'></th></span></q></dt></tr></i><div id='AHFZO'><tfoot id='AHFZO'></tfoot><dl id='AHFZO'><fieldset id='AHFZO'></fieldset></dl></div>
              2. <legend id='AHFZO'><style id='AHFZO'><dir id='AHFZO'><q id='AHFZO'></q></dir></style></legend>
                • 本文介绍了在 JavaScript 中使用两种方式散列 JSON 字符串以用于 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想获取一个 JSON 字符串并对其进行加密/散列/编码,以便将其放入 URL 中,使其类似于如下所示的内容:

                  I would like to take a JSON string and encrypt/hash/encode it so that I can put it into a URL so that it would resemble something such as seen below:

                  var stringToEncode = JSON.stringify({foo: 'baz', bar: [1,2,3,4,5], baz: {fizzle: 'buzz'}});
                  

                  'www.myrandomurl.com/someurl/123fas234asf1543rasfsafda'

                  然后我想获取该加密/散列/编码字符串,并将其解码回其原始 JSON 字符串,以便我可以使用它来绑定到单页 AngularJS 应用程序上的各种元素.

                  I would then like to take that encrypted/hashed/encoded string, and decode it back to its original JSON string so that I can use it to bind to various elements on a single-page AngularJS application.

                  JSON 字符串的内容不敏感,因此不需要安全性或复杂的散列.唯一的条件是它必须是一个URL/URI 'safe'"字符串,为了虚荣目的而进行哈希处理,如上所示.

                  The contents of the JSON string are not sensitive so security or complex hashing is not required. The only condition is that It needs to be a "URL/URI 'safe'" string that is hashed for vanity purposes like seen above.

                  我对加密/编码的了解有限,但是我考虑过简单地将字符串编码为 Base64 并再次解码.

                  I am limited in knowledge of encrypting/encoding however I have thought about simply encoding the string to Base64 and decoding it again.

                  这是最好的方法吗?如果没有,有什么更好的方法?

                  Is this the best approach? What is a better method if not?

                  推荐答案

                  使用 encodeURIComponent() 为 url 编码

                  Use encodeURIComponent() to encode it for the url

                  要解码,请使用 decodeURIComponent()函数

                  To decode use the decodeURIComponent() function

                  Base64 不是 URL 安全的,因为它可以包含非 url 字符,例如/+ -.(见这个问题)

                  Base64 is not URL safe since it can contain non url characters like / + -. (See this question)

                  如果您希望您的 url 与原始字符串不太相似,您可以先转换为 base64,然后通过从 base 64 解码和 covrrtibg 进行编码和反转

                  If you want your url to not be too similair to the original string you can first covert to base64 and then encode and reverse by decoding and covrrtibg back from base 64

                  // to url
                  encodeURIComponent(btoa(str))
                  
                  // from url
                  atob(decodeURIComponent(uri))
                  

                  这篇关于在 JavaScript 中使用两种方式散列 JSON 字符串以用于 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:对 base64 URI 支持的更好测试(我可以在 JS 中创建一个大的 base64 编码图像吗?) 下一篇:jpeg 的有效 base64 字符串

                  相关文章

                    <tfoot id='Hroh4'></tfoot>
                  1. <legend id='Hroh4'><style id='Hroh4'><dir id='Hroh4'><q id='Hroh4'></q></dir></style></legend>

                      <small id='Hroh4'></small><noframes id='Hroh4'>

                        <bdo id='Hroh4'></bdo><ul id='Hroh4'></ul>

                      <i id='Hroh4'><tr id='Hroh4'><dt id='Hroh4'><q id='Hroh4'><span id='Hroh4'><b id='Hroh4'><form id='Hroh4'><ins id='Hroh4'></ins><ul id='Hroh4'></ul><sub id='Hroh4'></sub></form><legend id='Hroh4'></legend><bdo id='Hroh4'><pre id='Hroh4'><center id='Hroh4'></center></pre></bdo></b><th id='Hroh4'></th></span></q></dt></tr></i><div id='Hroh4'><tfoot id='Hroh4'></tfoot><dl id='Hroh4'><fieldset id='Hroh4'></fieldset></dl></div>