var ber=require("asn1").Ber,_=require("../utils")._,PUBLIC_RSA_OID="1.2.840.113549.1.1.1",utils=require("../utils");const PRIVATE_OPENING_BOUNDARY="-----BEGIN PRIVATE KEY-----",PRIVATE_CLOSING_BOUNDARY="-----END PRIVATE KEY-----",PUBLIC_OPENING_BOUNDARY="-----BEGIN PUBLIC KEY-----",PUBLIC_CLOSING_BOUNDARY="-----END PUBLIC KEY-----";module.exports={privateExport:function(e,r){r=r||{};var t=e.n.toBuffer(),n=e.d.toBuffer(),i=e.p.toBuffer(),f=e.q.toBuffer(),u=e.dmp1.toBuffer(),a=e.dmq1.toBuffer(),I=e.coeff.toBuffer(),B=t.length+n.length+i.length+f.length+u.length+a.length+I.length+512,o=new ber.Writer({size:B});o.startSequence(),o.writeInt(0),o.writeBuffer(t,2),o.writeInt(e.e),o.writeBuffer(n,2),o.writeBuffer(i,2),o.writeBuffer(f,2),o.writeBuffer(u,2),o.writeBuffer(a,2),o.writeBuffer(I,2),o.endSequence();var S=new ber.Writer({size:B});return S.startSequence(),S.writeInt(0),S.startSequence(),S.writeOID(PUBLIC_RSA_OID),S.writeNull(),S.endSequence(),S.writeBuffer(o.buffer,4),S.endSequence(),"der"===r.type?S.buffer:PRIVATE_OPENING_BOUNDARY+"\n"+utils.linebrk(S.buffer.toString("base64"),64)+"\n"+PRIVATE_CLOSING_BOUNDARY},privateImport:function(e,r,t){var n;if("der"!==(t=t||{}).type){if(Buffer.isBuffer(r)&&(r=r.toString("utf8")),!_.isString(r))throw Error("Unsupported key format");var i=utils.trimSurroundingText(r,PRIVATE_OPENING_BOUNDARY,PRIVATE_CLOSING_BOUNDARY).replace("-----END PRIVATE KEY-----","").replace(/\s+|\n\r|\n|\r$/gm,"");n=Buffer.from(i,"base64")}else{if(!Buffer.isBuffer(r))throw Error("Unsupported key format");n=r}var f=new ber.Reader(n);if(f.readSequence(),f.readInt(0),new ber.Reader(f.readString(48,!0)).readOID(6,!0)!==PUBLIC_RSA_OID)throw Error("Invalid Public key format");var u=new ber.Reader(f.readString(4,!0));u.readSequence(),u.readString(2,!0),e.setPrivate(u.readString(2,!0),u.readString(2,!0),u.readString(2,!0),u.readString(2,!0),u.readString(2,!0),u.readString(2,!0),u.readString(2,!0),u.readString(2,!0))},publicExport:function(e,r){r=r||{};var t=e.n.toBuffer(),n=t.length+512,i=new ber.Writer({size:n});i.writeByte(0),i.startSequence(),i.writeBuffer(t,2),i.writeInt(e.e),i.endSequence();var f=new ber.Writer({size:n});return f.startSequence(),f.startSequence(),f.writeOID(PUBLIC_RSA_OID),f.writeNull(),f.endSequence(),f.writeBuffer(i.buffer,3),f.endSequence(),"der"===r.type?f.buffer:PUBLIC_OPENING_BOUNDARY+"\n"+utils.linebrk(f.buffer.toString("base64"),64)+"\n"+PUBLIC_CLOSING_BOUNDARY},publicImport:function(e,r,t){var n;if("der"!==(t=t||{}).type){if(Buffer.isBuffer(r)&&(r=r.toString("utf8")),_.isString(r)){var i=utils.trimSurroundingText(r,PUBLIC_OPENING_BOUNDARY,PUBLIC_CLOSING_BOUNDARY).replace(/\s+|\n\r|\n|\r$/gm,"");n=Buffer.from(i,"base64")}}else{if(!Buffer.isBuffer(r))throw Error("Unsupported key format");n=r}var f=new ber.Reader(n);if(f.readSequence(),new ber.Reader(f.readString(48,!0)).readOID(6,!0)!==PUBLIC_RSA_OID)throw Error("Invalid Public key format");var u=new ber.Reader(f.readString(3,!0));u.readByte(),u.readSequence(),e.setPublic(u.readString(2,!0),u.readString(2,!0))},autoImport:function(e,r){return/^[\S\s]*-----BEGIN PRIVATE KEY-----\s*(?=(([A-Za-z0-9+/=]+\s*)+))\1-----END PRIVATE KEY-----[\S\s]*$/g.test(r)?(module.exports.privateImport(e,r),!0):!!/^[\S\s]*-----BEGIN PUBLIC KEY-----\s*(?=(([A-Za-z0-9+/=]+\s*)+))\1-----END PUBLIC KEY-----[\S\s]*$/g.test(r)&&(module.exports.publicImport(e,r),!0)}};