Ruby字符串拆分

时间:2013-04-18 16:17:45

标签: ruby string split x509

我有一个长字符串如下:

  MIIEpAIBAAKCAQEAyUe3zAACNX50XRvh6CsL05yjyXZRpMKFqlKFN4QfrbbkGoGfY7NlfnicccHg1Kux8+BI5TxoOVF28Qa7DRiZRz6HnYRxvMJkKLhkcZMGhApefSS3Swk2kNw6bzVqMo20fidIb/FuDzRSn0E9UIXlOWFCFOdFFs3h4bhKkEagkW8swSo6dwJHoEzECxtn3U7Z6qnYbBJzxfY2H0O8CerdHHisIrjj9bofvYx4dRKUq98X16YPWIgutxJFiJN5XrWJ1IpYbaBCSxfdVQUe06C/CS47hGUXhO2GuWynW9u08yZRPHO+cbSE8W5/cq3vZrYBOX/LAdRKMJP76BX0nxV/3wIDAQABAoIBAQC7wFZFyExHMenBYFzdgCnL/nUZW3DUy057jfYNzwTtuYgU4YyYJ6Mo0cC8rCjYuD+KSVA15J8juEXGloysrqFrv3wpBn5BoOJkyXuIJakSScnR2gnPyIxJm+Q2EBE7jssqUeJT6VTMgfX/748OKZekaoCFlyTvHKTzNED5S6hDf1SlUhstGYbNYH6PhbID+ssdjQRt7Ys1s3EFLAxJIJeAeLTPi4dJVrBjBVShhl9BTzRRQH3sik7bdDHEq7PU6/Y63ZEPc7z6pdEb+sx8C4DKtRpRiUae1EooH7Ifk1DdF97wjOzA3VGeQFspy1ipmS1Xp+668Z0vrs6yFEUWLtUhAoGBAO8YMJuYyO59Dh/c+Y/5g+bSgQr2GZ5cFLlTFQ4+v3VPbe4Zezd5L0/vse/1qKEILwwHxRC5t3KSIysEYLmHLwskGBofH7mDAw0wcOttXGTAWiqzMWKae8CNCX8jNw9A3bq7j+e0Im+Bee0nXjhMFcOqiEO+yjAsvhUCWI0F+vmdAoGBANeDDo6KHboPu69i5UiH5TfnkLLQoovdXYgEmBFawJnMEc5EM6UhG+YGadHR7oNT5OM5K4WK3Uif3vNqspaQ0mD6gG514SZyATuKWy3+bgqgPfV8YOHsoGdc8I58qVXpUrSE07PXaH+N7kRtn3i0NHATLKJ4OKaucNolf0NoCpSrAoGALWupAEYcFcJIBqGR/1nlRtpCjXSHFWBkx3gTDeu5r21xcsQAdl9dL5BXe0Bt0nmdhmmLdEvzn55HdAqW3Wfi9fjbY0r6WeGbmMvVB8R0wucPugDkpti2HbQtU6u67/jgpnKvOdMsn4Y3RWgvK/64O72PuV7rRg+Z5J/XdqLmNLkCgYAGqmQJ3pW6yn/Ilet3idR6HSxpi3BrmQYqjdHSP5dTam9mjM94HQUK4UnmlnKlFzQuHveMotXjSCaSKsupQoNvQnA0emrjUIVfPRFC4+qtY3VnIVixxCrV55b9ITfvk07ejHkrx87Qmygyb52OEyzIBp/LyqId7alJ5f0ArbgeDwKBgQDDONXscChQQLPAFO9xLVLTU2JSCWcJAt4Dh8xhfDkGtYg+AcRb75+pVGW7jpg3311N8suuF2Eq3RWoCdvzUHD+TLUNMQrw7IfpKhtL1Po6KA/+G6xwh00ZkXL5o6AbmQ/fZW6/Yk5yx5PRxuwhLYCn6zVGJnENWmzkhVtawtHHFw==

我需要将它重新连接/拆分回一个看起来像这样的块。即将其恢复为正确的格式,以用作X509证书。

-----BEGIN CERTIFICATE-----
MIIGNTCCBR2gAwIBAgIKYWYgXQABAAACazANBgkqhkiG9w0BAQUFADBDMRMwEQYK
CZImiZPyLGQBGRYDY29tMRcwFQYKCZImiZPyLGQBGRYHY2puZGVtbzETMBEGA1UE
AxMKY2puZGVtby1DQTAeFw0xMzA0MTIxNjM4MTVaFw0xNDAzMDgxMjM0NDhaMHwx
CzAJBgNVBAYTAkdCMRIwEAYDVQQIEwlCZXJrc2hpcmUxEDAOBgNVBAcTB1NFIFRl
YW0xDzANBgNVBAoTBlZlbmFmaTERMA8GA1UEAxMIbmV3Y2VydDQxIzAhBgkqhkiG
9w0BCQEWFGphc29uLmJvdXJuZUB2ZWQuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOC
AQ8AMIIBCgKCAQEAv+ILegq1B81jOosGpySK+FO4+WQXJk4dyGWbC/MRw5JU2ShQ
Mjkpab8FDBgpZUmG6mvwrOBY8eK1NHvcognIPuHiQNm/c0YSqz/U8xTmBS0JRLd5
fDSkuhD5T7GKNa62Wg7dIPh3HtiKRTdC9HIBP3p/BFHz+jetgd8yM72Uzp0Lchpu
NJJDA5m0pgjbTOTlQ70tOq28kW8Hsko9p6cTQsVBOjxu7S6xSBq/g35NpgVZ3kic
jLEPAkdxAKWNORLOI3wD+AMiTp0DoV2fIw9do5a2XdbLy1QlTkRkXO+vM3LHCfAb
JfLZjrp11yP+wCz3NIJIi9lBDnYLo+6U2UOsZwIDAQABo4IC8DCCAuwwDgYDVR0P
AQH/BAQDAgWgMCkGA1UdJQQiMCAGCCsGAQUFBwMCBggrBgEFBQcDBAYKKwYBBAGC
NwoDBDAfBgNVHREEGDAWgRRqYXNvbi5ib3VybmVAdmVkLmNvbTAdBgNVHQ4EFgQU
SQCNiZ1456n8I0fi65oMWB8+MxEwHwYDVR0jBBgwFoAUmoUm8wJiYz+n+ZtdEoCU
i9XvXVgwgdMGA1UdHwSByzCByDCBxaCBwqCBv4aBvGxkYXA6Ly8vQ049Y2puZGVt   
by1DQSgxKSxDTj1XSU4tM0NGNDFOQlBUODUsQ049Q0RQLENOPVB1YmxpYyUyMEtl
eSUyMFNlcnZpY2VzLENOPVNlcnZpY2VzLENOPUNvbmZpZ3VyYXRpb24sREM9Y2pu
-----BEGIN CERTIFICATE-----

在Ruby中是否有内置的方法,或者有一种简单的方法可以做到这一点吗?

最好的问候

3 个答案:

答案 0 :(得分:7)

这会给你一个数组:

my_long_string.scan(/.{64}/)

如果您想将其与换行符放在一起:

my_long_string.scan(/.{64}/).join("\n")

答案 1 :(得分:2)

您可以使用Ruby的Base64模块重新编码字符串。该模块将考虑正确的填充:

require "base64"

str = "MIIEpAIBAAKCAQEAyUe3zAACNX50XRvh6CsL05yjyXZRpMKFqlKFN4QfrbbkGoGfY7Nlf..."
b64 = Base64.encode64(Base64.decode64(str))
pem = "-----BEGIN CERTIFICATE-----\n" << b64 << "-----END CERTIFICATE-----\n"

puts pem
# -----BEGIN CERTIFICATE-----
# MIIEpAIBAAKCAQEAyUe3zAACNX50XRvh6CsL05yjyXZRpMKFqlKFN4Qfrbbk
# GoGfY7NlfnicccHg1Kux8+BI5TxoOVF28Qa7DRiZRz6HnYRxvMJkKLhkcZMG
# hApefSS3Swk2kNw6bzVqMo20fidIb/FuDzRSn0E9UIXlOWFCFOdFFs3h4bhK
# ...
# U2JSCWcJAt4Dh8xhfDkGtYg+AcRb75+pVGW7jpg3311N8suuF2Eq3RWoCdvz
# UHD+TLUNMQrw7IfpKhtL1Po6KA/+G6xwh00ZkXL5o6AbmQ/fZW6/Yk5yx5PR
# xuwhLYCn6zVGJnENWmzkhVtawtHHFw==
# -----END CERTIFICATE-----

答案 2 :(得分:1)

您可以每隔64个字符拆分它们,例如:

a = "MIIGNTCCBR2gAwIBAgIKYWYgXQABAAACazANBgkqhkiG9w0BAQUFADBDMRMwEQYKCZImiZPyLGQBGRYDY29tMRcwFQYKCZImiZPyLGQBGRYHY2puZGVtbzETMBEGA1UE"
p a.scan(/.{1,64}/)

输出:

["MIIGNTCCBR2gAwIBAgIKYWYgXQABAAACazANBgkqhkiG9w0BAQUFADBDMRMwEQYK", "CZImiZPyLGQBGRYDY29tMRcwFQYKCZImiZPyLGQBGRYHY2puZGVtbzETMBEGA1UE"]

然后join使用新行\n

print a.scan(/.{1,64}/).join("\n")

输出:

MIIGNTCCBR2gAwIBAgIKYWYgXQABAAACazANBgkqhkiG9w0BAQUFADBDMRMwEQYK
CZImiZPyLGQBGRYDY29tMRcwFQYKCZImiZPyLGQBGRYHY2puZGVtbzETMBEGA1UE