Writing binary data to a file

I have an install script. This script opens a website and allows you to enter values ​​as well as upload a certificate for HTTPS.

But the result doesn't look like the input file. Here is my code that parses a multipart body

var parseForm = function (data) {
    var reg = /-----------------------------.{12}(\r\n|\r|\n)Content-Disposition: form-data; name="/
    var sections = data.split(reg)
    var result = {}
    for (sec in sections) {
        var values = sections[sec].split(/\b"(\r\n|\r|\n)(\r\n|\r|\n)/)
        if (/certificate"; filename/.test(values[0])) {
            var key = values[0].replace(/"; filename=".*/i, '')
            var value = values[3].replace(/Content-Type: application\/x-pkcs12(\r\n|\n|\r)+/i, '').replace(/-----------------------------.{12}--(\r\n|\r|\n)/, '')
            result[key] = value
        } else if (values[3]) {
            var key = values[0]
            var value = values[3].replace(/(\r\n|\r|\n)/, '')
            result[key] = value
        }
    }
    return result
}

      

And this is what I use to save the file:

fs.writeFile(path.join(__dirname, 'server.p12'), parameter[p], function (err) {
    console.error(err)
    console.error('please copy the file to "' + __dirname + '" and name it server.p12')
})

      

Any idea why it is outputting data incorrectly? Or better yet, how can I fix this? :)

EDIT

Maybe the problem is that the file is not loaded proberly. Is it possible?

EDIT 2

Buffer

was just a remnant of a Notepad ++ test to validate binary data :)

EDIT 3

Here is the original certificate:

MIIGugIBAzCCBnYGCSqGSIb3DQEHAaCCBmcEggZjMIIGXzCCA8AGCSqGSIb3DQEHAaCCA7EEggOtMIIDqTCCA6UGCyqGSIb3DQEMCgECoIICtjCCArIwHAYKKoZIhvcNAQwBAzAOBAjE3J2mwFcVBgICB9AEggKQvEMeRbebKva6/EiAdzdiFvjF4v5LXPSAL+kn/nLpnjJhpY2jyP+WHXFQD1i32we9SwquiDUiOP22bgQ2rhjoqgFjb4W1mRXdIULHLRMmW4Zy2Xa4tzhV9ddko2Iw/+aOEV6kB701FDGO8dw472eT7fZ9xCshm53hZ08xhbPk3FIZX0e//hc5Nqq/DbpgAg9mQdDaVKyxv4h2G0cH496443lLH1vwJ1US5pI6ErIIl39tacDzqfWtaQcYkzpql1GcQ3+vn3oPE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E1rcQ6n4f8vTwWTRkjkkUu2Q/gL7p7L1LASKbjmXSlu0MXRQX7GiO6veiKP8kBvL49epAot7/lPS8MAhG8Ask2cd1V5Eg5BqEXZ+9KAA5rNg+cpJ+sbU7sV0tix8RAyZneFP9+QQc07upavI28GuO74TMj/jdVEHlzytbff6W9yBc6QXGKEh2FAhhKBy9YRzNs6S1iHYl4/jygCe8irL4972H/8jYXJwfusDpwU28H3ROPlJ15zc+ZaF3I/DYSyq/8SaXaTvCXUuFhsC/lCquNAGyL6/b2Qcj7TsAGyO3WAw/o2WsFFm1RyKcq5sCfPb6hkrKzHcVP0pB+mpmfEOlnvnC2jR+Av1pp10HwBPT02m58poxNWrYdrsDAbzqp3dgQlOv777J6JqQkxdkt9N4wwY0E0L4szZtl6oIKtcK1tpGNVSzJK4bs1nKkLdQWjGIr2uLn8V95MNmBwTFKKiPIzxLMAr8hlTffaH6EWWuO5loVJ283Ojis4iybTeF60l838QVACOJEMtUITaxHEdMo7FFqyVbGgMP/DtDr6IQ/Zb+vN6nE+Yxz3b7pgvV1gTX1M6kAct/1AuYPLDIyFkbFSpyGaNuC30+DMtF8eDYnRDQRooAJN1ZIWmxfQ0rLuG+XqQFhCfotruJdvrPGOVnd7ON7nIjKQcKbvCtRyrWP7cIORPJz/dz0MyDkg1iF2D4YL1MVMDswHzAHBgUrDgMCGgQUsN+sOMkAQFF3MY7tENSKzkHgxhMEFAy/KjFIFw1dtV5wxho5AOgL8LiCAgIH0A==

      

And here is the multipart body:

-----------------------------7de17e2d670f22
Content-Disposition: form-data; name="COUCHDBSERVERHOST"

asd
-----------------------------7de17e2d670f22
Content-Disposition: form-data; name="COUCHDBSERVERPORT"

asd
-----------------------------7de17e2d670f22
Content-Disposition: form-data; name="COUCHDBSERVERUSERNAME"

asd
-----------------------------7de17e2d670f22
Content-Disposition: form-data; name="COUCHDBSERVERPASSWORD"

asd
-----------------------------7de17e2d670f22
Content-Disposition: form-data; name="PORT"

1234
-----------------------------7de17e2d670f22
Content-Disposition: form-data; name="DEFAULTDOMAIN"

asd
-----------------------------7de17e2d670f22
Content-Disposition: form-data; name="certificate"; filename="D:\serpentes\localhost.p12"
Content-Type: application/x-pkcs12

MO+/vQbvv70CAQMw77+9BnYGCSrvv71I77+977+9DQEHAe+/ve+/vQZnBO+/vQZjMO+/vQZfMO+/vQPvv70GCSrvv71I77+977+9DQEHAe+/ve+/vQPvv70E77+9A++/vTDvv70D77+9MO+/vQPvv70GCyrvv71I77+977+9DQEMCgEC77+977+9Au+/vTDvv70C77+9MBwGCirvv71I77+977+9DQEMAQMwDgQI77+93J3vv73vv71XFQYCAgfvv70E77+9Au+/ve+/vUMeRe+/ve+/vSrvv73vv73vv71I77+9dzdiFu+/ve+/ve+/ve+/vUtc77+977+9L++/vSfvv71y77+977+9MmHvv73vv73vv73vv73vv73vv70dcVAPWO+/ve+/vQfvv71LCu+/ve+/vTUiOO+/ve+/vW4ENu+/vRjvv73vv70BY2/vv73vv73vv70V77+9IULvv70tEyZb77+9cu+/vXbvv73vv704Ve+/ve+/vWTvv71iMO+/ve+/ve+/vRFe77+9B++/vTUUMe+/ve+/ve+/vTjvv71n77+977+977+9fe+/vSsh77+977+977+9Z08x77+977+977+977+9UhlfR++/ve+/vRc5Nu+/ve+/vQ3vv71gAg9mQe+/ve+/vVTvv73vv73vv73vv712G0cH77+93rjvv715Sx9b77+9J1US77+977+9OhLvv70I77+9f21p77+977+977+977+977+9aQcY77+9Omrvv71R77+9Q3/vv73vv716DxPvv73Vr3rvv70D77+9X0Hvv73Kge+/vT8XHw3vv73vv70MPi3vv71l77+9KC7vv70GBT7vv73vv70/77+977+977+977+9P0Tvv71zZu+/vToQd8Ou77+9Whsd77+977+9Se+/ve+/ve+/vVJScWBm77+9Oe+/vTk677+977+977+977+9Ve+/vW4Z5Z6HCyXvv73vv73vv73vv70Abl9uDBDvv73vv73vv73vv70N77+977+977+9DEfvv73vv73vv73vv71HRO+/vX8HRUHvv71z77+9Fe+/vUHvv70T77+9OX3vv70p77+977+977+9U+yrn++/vVnvv70177+977+9ZO+/vWsaZe+/vVoaJH7vv71ORu+/vSYFH++/vW1p77+977+977+977+9eO+/vTLvv71A77+9EV7vv706WO+/ve+/vQ12BWrvv73vv70LINGl77+9Xe+/vXcSdjc877+9Ne+/vUvvv71pNzoTa3Dvv71NBxZsYu+/ve+/ve+/ve+/vRUI77+977+9elRb77+977+9CO+/vXrvv71j77+9OO+/vXvvv73nmqjvv70gEjU2dSXvv71QNhjvv73vv73vv70z77+9JQDvv73vv73vv70u77+977+9G1Pvv73vv71fYe+/ve+/vV8NGe+/vQPvv73vv73vv70jUk0iIXLvv73vv70R77+9Xe+/ve+/vR/vv73vv70dD3EadxV877+9alLvv70D77+977+977+90LEpFu+/vRPvv71g77+977+977+9Zi/vv71/77+977+977+9D2Rn77+9NO+/vX/eju+/ve+/ve+/ve+/vVIwbxlcfhPvv71NPSTvv71B77+977+977+977+9fu+/ve+/vStmKO+/ve+/vTAt77+9Ie+/ve+/ve+/vRBB77+9DUM4Xgrvv70R77+9fe+/ve+/ve+/vTEqC++/vQoG77+9Zu+/vTg777+9KUjvv70Q77+9OO+/ve+tq++/vd2VPu+/ve+/vSFY77+9b++/ve+/ve+/ve+/ve+/vRTvv70u77+9IO+/vUDvv70O77+9ZWs3Te+/vQ3vv73vv71UPHPvv73vv73vv70T77+9B++/ve+/vVQCVnnvv70x77+977+9MBMGCSrvv71I77+977+9DQEJFTEGBAQBAAAAMF0GCSsGAQQB77+9NxEBMVAeTgBNAGkAYwByAG8AcwBvAGYAdAAgAFMAdAByAG8AbgBnACAAQwByAHkAcAB0AG8AZwByAGEAcABoAGkAYwAgAFAAcgBvAHYAaQBkAGUAcjBlBgkq77+9SO+/ve+/vQ0BCRQxWB5WAFAAdgBrAFQAbQBwADoAOABkADEANQBhADkAZABhAC0AOQA0ADgAMgAtADQANwAwADEALQA4AGMAOQA0AC0ANAA4AGIAMwA5ADkAMAA1ADkAZgBmAGUw77+9Au+/vQYJKu+/vUjvv73vv70NAQcG77+977+9Au+/vTDvv70C77+9AgEAMO+/vQJ9Bgkq77+9SO+/ve+/vQ0BBwEwHAYKKu+/vUjvv73vv70NAQwBBjAOBAjvv70BI8ugPdaVAgIH0IDvv70CUDLvv73vv73vv71q5qC6CGLvv73vv73vv73vv71877+977+9W2lB77+9zoEc77+977+977+977+9DO+/vW3vv71oNlxxEh1Hb0/vv71KEe+/ve+/ve+/vRLvv70LY++/ve+/vT8B77+9UO+/ve+/vSE177+977+9Ou+/ve+/ve+/ve+/vTwWTRkj77+9RS7vv70P77+9L++/vXsvUu+/vUjvv73vv73vv71077+977+9QxdFBe+/vRoj77+977+977+977+9P++/vQHvv73vv709eu+/vSjvv73vv73vv709LwwCEe+/vQLvv702ce+/vVXvv71IOQbvv70XZ++/vUoADms2D++/ve+/ve+/ve+/vW1O77+9V0ti77+977+9QMmZ77+9FO+/vX5BBzTvv73vv71a77+977+977+9Gu+/ve+/ve+/vTMj77+9N1UQeXPvv73vv73vv71/77+977+977+9FzpBce+/vRId77+9AhhKBy9YRzNs77+9LWId77+9eO+/vTzvv70J77+9Iu+/ve+/vT3vv71h77+977+9NhcnB++/ve+/vTpwU28H77+9E++/ve+/ve+/vXnvv73PmWhd77+977+9NhLKr++/vUnvv73vv71O77+977+9Uu+/vWHvv70v77+9Cu+/ve+/vQBs77+977+977+977+9Qe+/ve+/vU7vv70G77+977+977+9Aw/vv73vv71rBRZtUcinKu+/ve+/ve+/vT3vv73vv73vv73vv73vv70d77+9T9KQfu+/ve+/ve+/vRDvv71n77+9cO+/ve+/vR/vv73vv71aae+/vUHvv70E77+977+977+9bnzvv73vv71NWu+/vR3vv73vv73vv71vOu+/ve+/ve+/vRDvv73vv73vv73vv73vv716Ju+/vSTvv73vv70t77+977+9MO+/ve+/vQTQvizNm2Xvv73vv70K77+9wrXvv73vv73vv71VLO+/vSvvv73vv73vv71y77+9Le+/vRbvv71iK++/ve+/ve+/ve+/vV95MNmB77+9MUoqI++/ve+/vRLvv70C77+9Ie+/vTfvv71ofu+/vVlr77+977+9WhUnbzc6OO+/ve+/vSzvv71N77+9eu+/vV8377+9BUAI77+9RDLvv70ITe+/vUcR77+9KO+/vVFq77+9VsaA77+977+9Du+/ve+/ve+/ve+/vT9l77+977+9N++/ve+/ve+/ve+/vXPdvu+/ve+/ve+/vXXvv70177+9M++/vQBy77+977+9Au+/vQ8sMjIWRu+/vUrvv73vv71o24Lvv71P77+9Mu+/vXx4NidENBHvv73vv70JN1ZIWmxfQ0rLuG/vv73vv70BYQnvv70t77+977+9Xe+/ve+/ve+/vTlZ77+977+977+9e++/ve+/ve+/vUHCm++/vStRyrXvv73vv73vv70ORO+/vXPvv73vv73vv70zIO+/ve+/vVjvv73vv70+GC9TFTA7MB8wBwYFKw4DAhoEFO+/vd+sOO+/vQBAUXcx77+977+9ENSK77+9Qe+/ve+/vRMEFAzvv70qMUgXDV3vv71ecO+/vRo5AO+/vQvvv73vv73vv70CAgfvv70NCg==
-----------------------------7de17e2d670f22--

      

The certificates are encoded as bas64. I hope this helps. This is something encoded when I do base64 encoding and write it directly to the file it works. But I need to break it manually. All code is here

+3


source to share


2 answers


if your code works well on the encoded line, but does the job in a binary format, this is mainly due to the signing of the new lines . In short, there are several formats for line breaks.

this section covered here: Regular expression to match every new line character (\ n) inside the <content> tag

however , you can reformat your file by replacing these hidden characters with anything that suits you



this example uses notepad ++ https://superuser.com/questions/542719/how-to-find-and-replace-new-line-in-notepad

enter image description here

+1


source


For the record, this doesn't answer the question, it's just a workaround



I solved the problem. Since this is an installation script, it is clear that you are on the machine where you are installing the stuff.
So I ask the user to just copy the certificate. Other form data is submitted usingx-url-form-encoded

0


source







All Articles