gpt4 book ai didi

ruby - base64编码长度参数

转载 作者:数据小太阳 更新时间:2023-10-29 07:26:02 24 4
gpt4 key购买 nike

我正在解码一个 base64 字符串,修改它,然后用 Ruby 重新编码它。当我重新编码时出现的问题是 ruby​​ 编码库在 60 个左右的字符后添加了一个换行符。我如何告诉它每行没有最大字符数限制?

val = "QmFzZTY0IGlzIGEgZ2VuZXJpYyB0ZXJtIGZvciBhIG51bWJlciBvZiBzaW1pbGFyIGVuY29kaW5nIHNjaGVtZXMgdGhhdCBlbmNvZGUgYmluYXJ5IGRhdGEgYnkgdHJlYXRpbmcgaXQgbnVtZXJpY2FsbHkgYW5kIHRyYW5zbGF0aW5nIGl0IGludG8gYSBiYXNlIDY0IHJlcHJlc2VudGF0aW9uLiBUaGUgQmFzZTY0IHRlcm0gb3JpZ2luYXRlcyBmcm9tIGEgc3BlY2lmaWMgTUlNRSBjb250ZW50IHRyYW5zZmVyIGVuY29kaW5nLg0KDQpCYXNlNjQgZW5jb2Rpbmcgc2NoZW1lcyBhcmUgY29tbW9ubHkgdXNlZCB3aGVuIHRoZXJlIGlzIGEgbmVlZCB0byBlbmNvZGUgYmluYXJ5IGRhdGEgdGhhdCBuZWVkcyBiZSBzdG9yZWQgYW5kIHRyYW5zZmVycmVkIG92ZXIgbWVkaWEgdGhhdCBhcmUgZGVzaWduZWQgdG8gZGVhbCB3aXRoIHRleHR1YWwgZGF0YS4gVGhpcyBpcyB0byBlbnN1cmUgdGhhdCB0aGUgZGF0YSByZW1haW5zIGludGFjdCB3aXRob3V0IG1vZGlmaWNhdGlvbiBkdXJpbmcgdHJhbnNwb3J0LiBCYXNlNjQgaXMgdXNlZCBjb21tb25seSBpbiBhIG51bWJlciBvZiBhcHBsaWNhdGlvbnMgaW5jbHVkaW5nIGVtYWlsIHZpYSBNSU1FLCBhbmQgc3RvcmluZyBjb21wbGV4IGRhdGEgaW4gWE1MLg=="

decoded_val = Base64.decode64(val)
encoded_val = Base64.encode64(val)

#=> QmFzZTY0IGlzIGEgZ2VuZXJpYyB0ZXJtIGZvciBhIG51bWJlciBvZiBzaW1p
# bGFyIGVuY29kaW5nIHNjaGVtZXMgdGhhdCBlbmNvZGUgYmluYXJ5IGRhdGEg
# YnkgdHJlYXRpbmcgaXQgbnVtZXJpY2FsbHkgYW5kIHRyYW5zbGF0aW5nIGl0
# IGludG8gYSBiYXNlIDY0IHJlcHJlc2VudGF0aW9uLiBUaGUgQmFzZTY0IHRl
# cm0gb3JpZ2luYXRlcyBmcm9tIGEgc3BlY2lmaWMgTUlNRSBjb250ZW50IHRy
# YW5zZmVyIGVuY29kaW5nLg0KDQpCYXNlNjQgZW5jb2Rpbmcgc2NoZW1lcyBh
# cmUgY29tbW9ubHkgdXNlZCB3aGVuIHRoZXJlIGlzIGEgbmVlZCB0byBlbmNv
# ZGUgYmluYXJ5IGRhdGEgdGhhdCBuZWVkcyBiZSBzdG9yZWQgYW5kIHRyYW5z
# ZmVycmVkIG92ZXIgbWVkaWEgdGhhdCBhcmUgZGVzaWduZWQgdG8gZGVhbCB3
# aXRoIHRleHR1YWwgZGF0YS4gVGhpcyBpcyB0byBlbnN1cmUgdGhhdCB0aGUg
# ZGF0YSByZW1haW5zIGludGFjdCB3aXRob3V0IG1vZGlmaWNhdGlvbiBkdXJp
# bmcgdHJhbnNwb3J0LiBCYXNlNjQgaXMgdXNlZCBjb21tb25seSBpbiBhIG51
# bWJlciBvZiBhcHBsaWNhdGlvbnMgaW5jbHVkaW5nIGVtYWlsIHZpYSBNSU1F
# LCBhbmQgc3RvcmluZyBjb21wbGV4IGRhdGEgaW4gWE1MLg==

最佳答案

RFC 4648: The Base16, Base32, and Base64 Data Encodings有这样的话:

3.3. Interpretation of Non-Alphabet Characters in Encoded Data
[...]
Implementations MUST reject the encoded data if it containscharacters outside the base alphabet when interpreting base-encodeddata, unless the specification referring to this document explicitlystates otherwise. Such specifications may instead state, as MIMEdoes, that characters outside the base encoding alphabet shouldsimply be ignored when interpreting data ("be liberal in what youaccept"). Note that this means that any adjacent carriage return/line feed (CRLF) characters constitute "non-alphabet characters" andare ignored.

所以换行符很好,几乎所有内容都会忽略它们,即使它们不严格符合 RFC 4648。

另外,fine manual有这样的话:

encode64(bin)

Returns the Base64-encoded version of bin. This method complies with RFC 2045. Line feeds are added to every 60 encoded charactors [sic].

所以 60 个字符的行长度是有意指定的。如果你想要严格的 RFC 4648 Base64(即没有换行符),那么有 strict_encode64 :

strict_encode64(bin)

Returns the Base64-encoded version of bin. This method complies with RFC 4648. No line feeds are added.

所以你可以说 Base64.strict_encode64(val)以获得您正在寻找的输出。

作为引用,这里是 relevant section of RFC 2045 :

6.8. Base64 Content-Transfer-Encoding
[...]
The encoded output stream must be represented in lines of no morethan 76 characters each. All line breaks or other characters notfound in Table 1 must be ignored by decoding software.

所以 60 个字符的行长度有些随意,但自 60 < 76 起符合 RFC 2045。 .

关于ruby - base64编码长度参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13301708/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com