Comparing version 1.0.9 to 1.0.10
{ | ||
"name": "kscryp", | ||
"version": "1.0.9", | ||
"version": "1.0.10", | ||
"description": "Cryptography package with support for: JWT, RSA, MD5, SHA, Base64, HEX, JSON, Basic", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -191,1 +191,73 @@ # Ks Cryp | ||
The security of RSA relies on the fact that it is difficult to factor large prime numbers. However, as computing power has increased, it has become easier to break RSA encryption using brute force methods. To counter this, larger key sizes are now recommended for RSA encryption. | ||
## x509 | ||
In cryptography, X.509 is an [ITU](https://en.wikipedia.org/wiki/International_Telecommunication_Union) standard defining the format of public key certificates. | ||
### Generate x509 certificate | ||
```js | ||
const opts = { | ||
altNameIPs: ['127.0.0.1', '55.77.55.77'], | ||
altNameURIs: ['http://localhost', 'https://test.com'], | ||
validityDays: 300, | ||
length: 2048, | ||
data: { | ||
commonName: 'my.test.com', | ||
stateOrProvinceName: 'Barcelona', | ||
countryName: 'ES', | ||
localityName: 'Barcelona', | ||
organizationName: 'Aircraft', | ||
organizationNameShort: 'TesTas' | ||
} | ||
}; | ||
const cert = await KsCryp.generate("x509", opts); | ||
const fs = require("fs"); | ||
fs.writeFileSync("x509-cert.pem", cert.publicKey); | ||
fs.writeFileSync("x509-key.pem", cert.privateKey); | ||
``` | ||
X.509 certificates bind an identity to a public key using a digital signature. In the X.509 system, there are two types of certificates. The first is a CA certificate. The second is an end-entity certificate. A CA certificate can issue other certificates. The top level, self-signed CA certificate is sometimes called the Root CA certificate. Other CA certificates are called intermediate CA or subordinate CA certificates. An end-entity certificate identifies the user, like a person, organization or business. An end-entity certificate cannot issue other certificates. An end-entity certificate is sometimes called a leaf certificate since no other certificates can be issued below it. | ||
## Json | ||
JSON (JavaScript Object Notation) is a lightweight data interchange format that uses a human-readable text structure to represent and transmit structured data. It consists of key-value pairs organized in objects and supports arrays, numbers, strings, booleans, and null values. JSON is widely used for data exchange between applications due to its simplicity, versatility, and ease of parsing across various programming languages. | ||
### Decoding a string in JSON format with special characters | ||
The issue with special characters like line breaks or double quotes when decoding a JSON string with the JSON.parse function is that they can cause parsing errors and disrupt the expected structure of the JSON data. | ||
```js | ||
const srtObj = ` | ||
"{\\"name\\": \\"PIL W2F\\", | ||
\\"des\\":\\"PT | ||
PAL\\"}" | ||
`; | ||
const resObj = target.decode(srtObj, "json", { defaultValue: null, strict: true }); | ||
console.log( | ||
resObj.name === "PIL W2F", | ||
resObj.des === "PT PAL" | ||
) | ||
``` | ||
### Encoding objects with cyclic dependencies | ||
The issue of cyclic dependency arises when using the JSON.stringify function to encode objects that have circular references, causing the process to enter an infinite loop and resulting in incomplete or erroneous JSON representation. | ||
```js | ||
const reqObj = { "age": 1, "name": "test", "live": true, "work": { "lat": 1 } }; | ||
reqObj.ref = reqObj; | ||
``` | ||
Using the native method JSON.stringify you should get the following error: | ||
``` | ||
Error: Converting circular structure to JSON | ||
--> starting at object with constructor 'Object' | ||
--- property 'ref' closes the circle | ||
``` | ||
To solve the above problem, we can do the following: | ||
```js | ||
const reqStr = target.encode(reqObj, "json"); | ||
console.log( | ||
reqStr === '{"age":1,"name":"test","live":true,"work":{"lat":1}}' | ||
); | ||
``` |
@@ -33,2 +33,9 @@ const KsDriver = require("../KsDriver"); | ||
options.validType = "string"; | ||
if (typeof (value) === "string" && options.strict) { | ||
value = value | ||
.replace(/\\"/g, '"') | ||
.replace(/"{/g, '{') | ||
.replace(/}"/g, '}') | ||
.replace(/\\r|\r|\n|\\n/g, ""); | ||
} | ||
return this.respond(value, null, options) ?? JSON.parse(value); | ||
@@ -35,0 +42,0 @@ } |
@@ -29,2 +29,20 @@ const target = require("../") | ||
it('decode valid data with special characters', () => { | ||
const srtObj1 = '"{\\"W2F\\":\\"PIL W2F\\",\\"PAL\\":\\"PT PAL\\"}"'; | ||
const srtObj2 = ` | ||
"{\\"W2F\\": \\"PIL W2F\\", | ||
\\"PAL\\":\\"PT | ||
PAL\\"}" | ||
`; | ||
const resObj1 = target.decode(srtObj1, "json", { defaultValue: null, strict: true }); | ||
const resObj2 = target.decode(srtObj2, "json", { defaultValue: null, strict: true }); | ||
const resObj3 = target.decode(srtObj2, "json"); | ||
expect(resObj1).toBeInstanceOf(Object); | ||
expect(resObj2).toBeInstanceOf(Object); | ||
expect(resObj1.W2F).toBe("PIL W2F"); | ||
expect(resObj2.W2F).toBe("PIL W2F"); | ||
expect(resObj3).toBe(srtObj2); | ||
}); | ||
it('encode wrong data', () => { | ||
@@ -47,2 +65,11 @@ expect(target.encode("{.}", "json")).toBe("{.}"); | ||
}); | ||
it('encode objects with cyclic dependencies', () => { | ||
const resStr = '{"age":1,"name":"test","live":true,"work":{"lat":1}}'; | ||
const reqObj = { "age": 1, "name": "test", "live": true, "work": { "lat": 1 } }; | ||
reqObj.ref = reqObj; | ||
const reqStr = target.encode(reqObj, "json"); | ||
expect(typeof reqStr).toBe("string"); | ||
expect(reqStr).toBe(resStr); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
51045
928
262