From da894c08551f9c59bfa2e5e3c723b691506a6f59 Mon Sep 17 00:00:00 2001 From: "Visal .In" Date: Fri, 30 Oct 2020 09:21:38 +0700 Subject: [PATCH 1/5] Add basic BER writer --- src/utility/asn1.ts | 82 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/utility/asn1.ts diff --git a/src/utility/asn1.ts b/src/utility/asn1.ts new file mode 100644 index 0000000..db18047 --- /dev/null +++ b/src/utility/asn1.ts @@ -0,0 +1,82 @@ +function createSizeBuffer(size: number): Uint8Array { + if (size <= 127) return new Uint8Array([size]); + + const bytes = []; + while (size > 0) { + bytes.push(size & 0xff); + size = size >> 8; + } + + bytes.reverse(); + return new Uint8Array([0x80 + bytes.length, ...bytes]); +} + +export class BER { + public static createSequence(children: Uint8Array[]): Uint8Array { + // Combine the total size of its children + const size = children.reduce( + (accumlatedSize, child) => accumlatedSize + child.length, + 0 + ); + + return new Uint8Array([ + 0x30, + ...createSizeBuffer(size), + ...children.reduce( + (buffer, child) => [...buffer, ...child], + [] + ), + ]); + } + + public static createNull() { + return new Uint8Array([0x05, 0x00]); + } + + public static createBoolean(value: boolean) { + return new Uint8Array([0x01, 0x01, value ? 0x01 : 0x00]); + } + + public static createInteger(value: bigint | number): Uint8Array { + if (typeof value === "number") return BER.createBigInteger(BigInt(value)); + return BER.createBigInteger(value); + } + + public static createBigInteger(value: bigint): Uint8Array { + if (value === 0n) return new Uint8Array([0x02, 0x01, 0x00]); + + const isNegative = value < 0; + const content: number[] = []; + let n = isNegative ? -value : value; + + while (n > 0n) { + content.push(Number(n & 255n)); + n = n >> 8n; + } + + if (!isNegative) { + if (content[content.length - 1] & 0x80) content.push(0x00); + } else { + // Flipping the bit + for (let i = 0; i < content.length; i++) content[i] = 256 - content[i]; + if (!(content[content.length - 1] & 0x80)) content.push(0xff); + } + + content.reverse(); + + return new Uint8Array([ + 0x02, + ...createSizeBuffer(content.length), + ...content, + ]); + } + + public static createBitString(value: Uint8Array): Uint8Array { + return new Uint8Array([ + 0x03, + ...createSizeBuffer(value.length + 1), + 0x00, + ...value, + ]); + } +} From c0408297b8a56e30d560b0a1ab9833edbb33d752 Mon Sep 17 00:00:00 2001 From: "Visal .In" Date: Fri, 30 Oct 2020 09:21:58 +0700 Subject: [PATCH 2/5] Switch handwritten BER to BER writer --- src/rsa/export_key.ts | 153 ++++++++++--------------------- tests/rsa/rsa.export_key.test.ts | 13 +-- 2 files changed, 57 insertions(+), 109 deletions(-) diff --git a/src/rsa/export_key.ts b/src/rsa/export_key.ts index d3c73d6..b092d4f 100644 --- a/src/rsa/export_key.ts +++ b/src/rsa/export_key.ts @@ -1,37 +1,7 @@ import { RSAKeyParams } from "./common.ts"; import { bignum_to_byte } from "../helper.ts"; import { encode } from "./../../src/utility/encode.ts"; - -function ber_size_bytes(size: number): number[] { - // The BER Length - // The second component in the TLV structure of a BER element is the length. - // This specifies the size in bytes of the encoded value. For the most part, - // this uses a straightforward binary encoding of the integer value - // (for example, if the encoded value is five bytes long, then it is encoded as - // 00000101 binary, or 0x05 hex), but if the value is longer than 127 bytes then - // it is necessary to use multiple bytes to encode the length. In that case, the - // first byte has the leftmost bit set to one and the remaining seven bits are - // used to specify the number of bytes required to encode the full length. For example, - // if there are 500 bytes in the length (hex 0x01F4), then the encoded length will actually - // consist of three bytes: 82 01 F4. - // - // Note that there is an alternate form for encoding the length called the indefinite form. - // In this mechanism, only a part of the length is given at a time, similar to the chunked encoding - // that is available in HTTP 1.1. However, this form is not used in LDAP, as specified in RFC 2251 - // section 5.1. - // https://docs.oracle.com/cd/E19476-01/821-0510/def-basic-encoding-rules.html - - if (size <= 127) return [size]; - - const bytes = []; - while (size > 0) { - bytes.push(size & 0xff); - size = size >> 8; - } - - bytes.reverse(); - return [0x80 + bytes.length, ...bytes]; -} +import { BER } from "../utility/asn1.ts"; function add_line_break(base64_str: string): string { const lines = []; @@ -42,80 +12,57 @@ function add_line_break(base64_str: string): string { return lines.join("\n"); } -function ber_generate_integer_list(order: number[][]) { - let content: number[] = []; - - for (const item of order) { - if ((item[0] & 0x80) > 0) { - content = content.concat( - [0x02, ...ber_size_bytes(item.length + 1), 0x0, ...item], - ); - } else { - content = content.concat( - [0x02, ...ber_size_bytes(item.length), ...item], - ); - } - } - - return content; -} - export function rsa_export_pkcs8_public(key: RSAKeyParams) { - const n = bignum_to_byte(key.n); - const e = bignum_to_byte(key.e || 0n); - - // deno-fmt-ignore - const other = [0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00]; - - // Key sequence - const content = ber_generate_integer_list([n, e]); - const keySequence = [ - 0x30, - ...ber_size_bytes(content.length), - ...content, - ]; - - // Bitstring - const bitString = [ - 0x03, - ...ber_size_bytes(keySequence.length + 1), - 0x00, - ...keySequence, - ]; - - const ber = [ - 0x30, - ...ber_size_bytes(other.length + bitString.length), - ...other, - ...bitString, - ]; - - return "-----BEGIN PUBLIC KEY-----\n" + - add_line_break(encode.binary(ber).base64()) + - "\n-----END PUBLIC KEY-----\n"; + const content = BER.createSequence([ + BER.createSequence([ + new Uint8Array([ + 0x06, + 0x09, + 0x2a, + 0x86, + 0x48, + 0x86, + 0xf7, + 0x0d, + 0x01, + 0x01, + 0x01, + ]), + BER.createNull(), + ]), + BER.createBitString( + BER.createSequence([ + BER.createInteger(key.n), + BER.createInteger(key.e || 0n), + ]) + ), + ]); + + return ( + "-----BEGIN PUBLIC KEY-----\n" + + add_line_break(encode.binary(content).base64()) + + "\n-----END PUBLIC KEY-----\n" + ); } export function rsa_export_pkcs8_private(key: RSAKeyParams) { - const n = bignum_to_byte(key.n); - const e = bignum_to_byte(key.e || 0n); - const d = bignum_to_byte(key.d || 0n); - const q = bignum_to_byte(key.q || 0n); - const p = bignum_to_byte(key.p || 0n); - const dp = bignum_to_byte(key.dp || 0n); - const dq = bignum_to_byte(key.dq || 0n); - const qi = bignum_to_byte(key.qi || 0n); - - const content = ber_generate_integer_list([n, e, d, p, q, dp, dq, qi]); - - const ber = encode.binary([ - 0x30, - ...ber_size_bytes(content.length + 3), - 0x02, - 0x01, - 0x00, - ...content, - ]).base64(); - - return "-----BEGIN RSA PRIVATE KEY-----\n" + add_line_break(ber) + - "\n-----END RSA PRIVATE KEY-----\n"; + const content = BER.createSequence([ + BER.createInteger(0), + BER.createInteger(key.n), + BER.createInteger(key.e || 0n), + BER.createInteger(key.d || 0n), + BER.createInteger(key.p || 0n), + BER.createInteger(key.q || 0n), + BER.createInteger(key.dp || 0n), + BER.createInteger(key.dq || 0n), + BER.createInteger(key.qi || 0n), + ]); + + const ber = encode.binary(content).base64(); + + return ( + "-----BEGIN RSA PRIVATE KEY-----\n" + + add_line_break(ber) + + "\n-----END RSA PRIVATE KEY-----\n" + ); } diff --git a/tests/rsa/rsa.export_key.test.ts b/tests/rsa/rsa.export_key.test.ts index 7f86b53..824c98d 100644 --- a/tests/rsa/rsa.export_key.test.ts +++ b/tests/rsa/rsa.export_key.test.ts @@ -1,10 +1,9 @@ import { RSA } from "./../../mod.ts"; -import { - assertEquals, -} from "https://deno.land/std@0.63.0/testing/asserts.ts"; +import { assertEquals } from "https://deno.land/std@0.63.0/testing/asserts.ts"; const privateKeyRaw = Deno.readTextFileSync("./tests/rsa/private.pem"); -const publicKeyRaw = "-----BEGIN PUBLIC KEY-----\n" + +const publicKeyRaw = + "-----BEGIN PUBLIC KEY-----\n" + "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArlKJ591/fYCKhdflQSNi\n" + "xBhWutUtW5y3l5vFzTxiKE4e9jykJ0Sr7U6GkwjmvplTV7Wgx4zhRr3tYrMqmQ+s\n" + "/byRK3f2bb+zXF9+fnKGuP7Fp2oYprW3MKxKgNxjRzmx2x7LaV11dHFQv6oigeV2\n" + @@ -22,7 +21,8 @@ Deno.test("RSA - PKCS8 to PKCS8", async () => { Deno.test("RSA - JWK to PKCS8", async () => { const jwk = { kty: "RSA", - n: "rlKJ591_fYCKhdflQSNixBhWutUtW5y3l5vFzTxiKE4e9jykJ0Sr7U6GkwjmvplTV7Wgx4zhRr3tYrMqmQ-s_byRK3f2bb-zXF9-fnKGuP7Fp2oYprW3MKxKgNxjRzmx2x7LaV11dHFQv6oigeV2cyY5XB_GnEWUyHY7fCJIJIRdxuskt-77NAU0vrA_ntbWzFFsPP5xWJ8ns_ojTvwu-LT--fpBD3X1nTUR_LzlRgGxGqPHYRCHvY8B2FSPL8ukqfXI3LkvCM77zeR5lwPqIqDFVWcP6TNsOXccqDtBiA3-A6TS3nGmOu3NbZdefkzJlXq2D0xuW6ql0WqBM0Vubw", + n: + "rlKJ591_fYCKhdflQSNixBhWutUtW5y3l5vFzTxiKE4e9jykJ0Sr7U6GkwjmvplTV7Wgx4zhRr3tYrMqmQ-s_byRK3f2bb-zXF9-fnKGuP7Fp2oYprW3MKxKgNxjRzmx2x7LaV11dHFQv6oigeV2cyY5XB_GnEWUyHY7fCJIJIRdxuskt-77NAU0vrA_ntbWzFFsPP5xWJ8ns_ojTvwu-LT--fpBD3X1nTUR_LzlRgGxGqPHYRCHvY8B2FSPL8ukqfXI3LkvCM77zeR5lwPqIqDFVWcP6TNsOXccqDtBiA3-A6TS3nGmOu3NbZdefkzJlXq2D0xuW6ql0WqBM0Vubw", e: "AQAB", }; @@ -32,7 +32,8 @@ Deno.test("RSA - JWK to PKCS8", async () => { Deno.test("RSA - PKCS8 to JWK", async () => { const jwk = { kty: "RSA", - n: "rlKJ591_fYCKhdflQSNixBhWutUtW5y3l5vFzTxiKE4e9jykJ0Sr7U6GkwjmvplTV7Wgx4zhRr3tYrMqmQ-s_byRK3f2bb-zXF9-fnKGuP7Fp2oYprW3MKxKgNxjRzmx2x7LaV11dHFQv6oigeV2cyY5XB_GnEWUyHY7fCJIJIRdxuskt-77NAU0vrA_ntbWzFFsPP5xWJ8ns_ojTvwu-LT--fpBD3X1nTUR_LzlRgGxGqPHYRCHvY8B2FSPL8ukqfXI3LkvCM77zeR5lwPqIqDFVWcP6TNsOXccqDtBiA3-A6TS3nGmOu3NbZdefkzJlXq2D0xuW6ql0WqBM0Vubw", + n: + "rlKJ591_fYCKhdflQSNixBhWutUtW5y3l5vFzTxiKE4e9jykJ0Sr7U6GkwjmvplTV7Wgx4zhRr3tYrMqmQ-s_byRK3f2bb-zXF9-fnKGuP7Fp2oYprW3MKxKgNxjRzmx2x7LaV11dHFQv6oigeV2cyY5XB_GnEWUyHY7fCJIJIRdxuskt-77NAU0vrA_ntbWzFFsPP5xWJ8ns_ojTvwu-LT--fpBD3X1nTUR_LzlRgGxGqPHYRCHvY8B2FSPL8ukqfXI3LkvCM77zeR5lwPqIqDFVWcP6TNsOXccqDtBiA3-A6TS3nGmOu3NbZdefkzJlXq2D0xuW6ql0WqBM0Vubw", e: "AQAB", }; From b1b16574a54fe69fed47041a44da39c680204035 Mon Sep 17 00:00:00 2001 From: "Visal .In" Date: Sat, 5 Dec 2020 18:21:21 +0700 Subject: [PATCH 3/5] Mass fmt --- src/aes/aes_js.ts | 5 +---- src/rsa/eme_oaep.ts | 2 +- src/rsa/export_key.ts | 2 +- src/rsa/import_key.ts | 2 +- src/rsa/mod.ts | 2 +- src/rsa/rsa_internal.ts | 4 ++-- src/rsa/rsa_js.ts | 6 +++--- src/rsa/rsa_key.ts | 2 +- src/utility/asn1.ts | 4 ++-- tests/aes/aes.openssl.test.ts | 4 +--- tests/aes/aes.test.ts | 4 +--- tests/hmac/hmac.test.ts | 4 +--- tests/otp/otp.test.ts | 4 +--- tests/rsa/rsa.export_key.test.ts | 9 +++------ tests/rsa/rsa.import_key.test.ts | 4 +--- tests/rsa/rsa.test.ts | 4 +--- tests/rsa/rsa.verify.test.ts | 6 ++---- tests/utility/encode.test.ts | 4 +--- 18 files changed, 25 insertions(+), 47 deletions(-) diff --git a/src/aes/aes_js.ts b/src/aes/aes_js.ts index 0fc818c..899454a 100644 --- a/src/aes/aes_js.ts +++ b/src/aes/aes_js.ts @@ -3,10 +3,7 @@ // https://link.springer.com/content/pdf/10.1007/3-540-36400-5_13.pdf // http://asmaes.sourceforge.net/rijndael/rijndaelImplementation.pdf // https://www.movable-type.co.uk/scripts/aes.html -import { - BlockCiper, - BlockCiperOperation, -} from "./block_ciper_operator.ts"; +import { BlockCiper, BlockCiperOperation } from "./block_ciper_operator.ts"; import { BlockCiperConfig } from "./common.ts"; import { AESBase } from "./aes_base.ts"; diff --git a/src/rsa/eme_oaep.ts b/src/rsa/eme_oaep.ts index 80b49e9..64d955c 100644 --- a/src/rsa/eme_oaep.ts +++ b/src/rsa/eme_oaep.ts @@ -1,6 +1,6 @@ import { createHash } from "./../hash.ts"; import { mgf1 } from "./primitives.ts"; -import { concat, xor, random_bytes } from "./../helper.ts"; +import { concat, random_bytes, xor } from "./../helper.ts"; /** * https://tools.ietf.org/html/rfc3447#page-10 diff --git a/src/rsa/export_key.ts b/src/rsa/export_key.ts index b092d4f..db01266 100644 --- a/src/rsa/export_key.ts +++ b/src/rsa/export_key.ts @@ -34,7 +34,7 @@ export function rsa_export_pkcs8_public(key: RSAKeyParams) { BER.createSequence([ BER.createInteger(key.n), BER.createInteger(key.e || 0n), - ]) + ]), ), ]); diff --git a/src/rsa/import_key.ts b/src/rsa/import_key.ts index e4e37b9..50967e8 100644 --- a/src/rsa/import_key.ts +++ b/src/rsa/import_key.ts @@ -1,6 +1,6 @@ import { encode } from "./../../src/utility/encode.ts"; import { JSONWebKey, RSAKeyParams } from "./common.ts"; -import { get_key_size, base64_to_binary } from "../helper.ts"; +import { base64_to_binary, get_key_size } from "../helper.ts"; import { ber_decode, ber_simple } from "./basic_encoding_rule.ts"; import { os2ip } from "./primitives.ts"; diff --git a/src/rsa/mod.ts b/src/rsa/mod.ts index 02eab87..0e281c2 100644 --- a/src/rsa/mod.ts +++ b/src/rsa/mod.ts @@ -1,4 +1,4 @@ -import { RSAOption, RSASignOption, JSONWebKey } from "./common.ts"; +import { JSONWebKey, RSAOption, RSASignOption } from "./common.ts"; import { WebCryptoRSA } from "./rsa_wc.ts"; import { PureRSA } from "./rsa_js.ts"; import { RawBinary } from "../binary.ts"; diff --git a/src/rsa/rsa_internal.ts b/src/rsa/rsa_internal.ts index 6154da4..093bfb1 100644 --- a/src/rsa/rsa_internal.ts +++ b/src/rsa/rsa_internal.ts @@ -1,6 +1,6 @@ import { power_mod } from "./../math.ts"; -import { eme_oaep_encode, eme_oaep_decode } from "./eme_oaep.ts"; -import { os2ip, i2osp } from "./primitives.ts"; +import { eme_oaep_decode, eme_oaep_encode } from "./eme_oaep.ts"; +import { i2osp, os2ip } from "./primitives.ts"; import { concat, random_bytes } from "./../helper.ts"; import { ber_decode, ber_simple } from "./basic_encoding_rule.ts"; import { RawBinary } from "../binary.ts"; diff --git a/src/rsa/rsa_js.ts b/src/rsa/rsa_js.ts index ee7730b..48b7268 100644 --- a/src/rsa/rsa_js.ts +++ b/src/rsa/rsa_js.ts @@ -1,10 +1,10 @@ import { - rsa_oaep_encrypt, - rsa_pkcs1_encrypt, rsa_oaep_decrypt, + rsa_oaep_encrypt, rsa_pkcs1_decrypt, - rsa_pkcs1_verify, + rsa_pkcs1_encrypt, rsa_pkcs1_sign, + rsa_pkcs1_verify, } from "./rsa_internal.ts"; import { RawBinary } from "./../binary.ts"; import { RSAOption, RSASignOption } from "./common.ts"; diff --git a/src/rsa/rsa_key.ts b/src/rsa/rsa_key.ts index 080d936..b29ca4e 100644 --- a/src/rsa/rsa_key.ts +++ b/src/rsa/rsa_key.ts @@ -1,4 +1,4 @@ -import { RSAKeyParams, JSONWebKey } from "./common.ts"; +import { JSONWebKey, RSAKeyParams } from "./common.ts"; import { encode } from "./../../src/utility/encode.ts"; import { rsa_export_pkcs8_private, diff --git a/src/utility/asn1.ts b/src/utility/asn1.ts index db18047..226e7e0 100644 --- a/src/utility/asn1.ts +++ b/src/utility/asn1.ts @@ -16,7 +16,7 @@ export class BER { // Combine the total size of its children const size = children.reduce( (accumlatedSize, child) => accumlatedSize + child.length, - 0 + 0, ); return new Uint8Array([ @@ -24,7 +24,7 @@ export class BER { ...createSizeBuffer(size), ...children.reduce( (buffer, child) => [...buffer, ...child], - [] + [], ), ]); } diff --git a/tests/aes/aes.openssl.test.ts b/tests/aes/aes.openssl.test.ts index 2addc7f..b7de802 100644 --- a/tests/aes/aes.openssl.test.ts +++ b/tests/aes/aes.openssl.test.ts @@ -1,6 +1,4 @@ -import { - assertEquals, -} from "https://deno.land/std@0.63.0/testing/asserts.ts"; +import { assertEquals } from "https://deno.land/std@0.63.0/testing/asserts.ts"; import { AES } from "./../../mod.ts"; Deno.test("AES - Decryption AES-128-CBC (OpenSSL)", async () => { diff --git a/tests/aes/aes.test.ts b/tests/aes/aes.test.ts index ab92d8c..01eba1e 100644 --- a/tests/aes/aes.test.ts +++ b/tests/aes/aes.test.ts @@ -1,6 +1,4 @@ -import { - assertEquals, -} from "https://deno.land/std@0.63.0/testing/asserts.ts"; +import { assertEquals } from "https://deno.land/std@0.63.0/testing/asserts.ts"; import { AES } from "./../../mod.ts"; import { RawBinary } from "../../src/binary.ts"; diff --git a/tests/hmac/hmac.test.ts b/tests/hmac/hmac.test.ts index 9665147..231072c 100644 --- a/tests/hmac/hmac.test.ts +++ b/tests/hmac/hmac.test.ts @@ -1,6 +1,4 @@ -import { - assertEquals, -} from "https://deno.land/std@0.63.0/testing/asserts.ts"; +import { assertEquals } from "https://deno.land/std@0.63.0/testing/asserts.ts"; import { hmac } from "./../../mod.ts"; Deno.test("Testing HMAC", () => { diff --git a/tests/otp/otp.test.ts b/tests/otp/otp.test.ts index ee18c52..b001d7b 100644 --- a/tests/otp/otp.test.ts +++ b/tests/otp/otp.test.ts @@ -1,6 +1,4 @@ -import { - assertEquals, -} from "https://deno.land/std@0.63.0/testing/asserts.ts"; +import { assertEquals } from "https://deno.land/std@0.63.0/testing/asserts.ts"; import { TOTP } from "./../../src/otp/totp.ts"; Deno.test("Testing TOTP", () => { diff --git a/tests/rsa/rsa.export_key.test.ts b/tests/rsa/rsa.export_key.test.ts index 824c98d..80dbd52 100644 --- a/tests/rsa/rsa.export_key.test.ts +++ b/tests/rsa/rsa.export_key.test.ts @@ -2,8 +2,7 @@ import { RSA } from "./../../mod.ts"; import { assertEquals } from "https://deno.land/std@0.63.0/testing/asserts.ts"; const privateKeyRaw = Deno.readTextFileSync("./tests/rsa/private.pem"); -const publicKeyRaw = - "-----BEGIN PUBLIC KEY-----\n" + +const publicKeyRaw = "-----BEGIN PUBLIC KEY-----\n" + "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArlKJ591/fYCKhdflQSNi\n" + "xBhWutUtW5y3l5vFzTxiKE4e9jykJ0Sr7U6GkwjmvplTV7Wgx4zhRr3tYrMqmQ+s\n" + "/byRK3f2bb+zXF9+fnKGuP7Fp2oYprW3MKxKgNxjRzmx2x7LaV11dHFQv6oigeV2\n" + @@ -21,8 +20,7 @@ Deno.test("RSA - PKCS8 to PKCS8", async () => { Deno.test("RSA - JWK to PKCS8", async () => { const jwk = { kty: "RSA", - n: - "rlKJ591_fYCKhdflQSNixBhWutUtW5y3l5vFzTxiKE4e9jykJ0Sr7U6GkwjmvplTV7Wgx4zhRr3tYrMqmQ-s_byRK3f2bb-zXF9-fnKGuP7Fp2oYprW3MKxKgNxjRzmx2x7LaV11dHFQv6oigeV2cyY5XB_GnEWUyHY7fCJIJIRdxuskt-77NAU0vrA_ntbWzFFsPP5xWJ8ns_ojTvwu-LT--fpBD3X1nTUR_LzlRgGxGqPHYRCHvY8B2FSPL8ukqfXI3LkvCM77zeR5lwPqIqDFVWcP6TNsOXccqDtBiA3-A6TS3nGmOu3NbZdefkzJlXq2D0xuW6ql0WqBM0Vubw", + n: "rlKJ591_fYCKhdflQSNixBhWutUtW5y3l5vFzTxiKE4e9jykJ0Sr7U6GkwjmvplTV7Wgx4zhRr3tYrMqmQ-s_byRK3f2bb-zXF9-fnKGuP7Fp2oYprW3MKxKgNxjRzmx2x7LaV11dHFQv6oigeV2cyY5XB_GnEWUyHY7fCJIJIRdxuskt-77NAU0vrA_ntbWzFFsPP5xWJ8ns_ojTvwu-LT--fpBD3X1nTUR_LzlRgGxGqPHYRCHvY8B2FSPL8ukqfXI3LkvCM77zeR5lwPqIqDFVWcP6TNsOXccqDtBiA3-A6TS3nGmOu3NbZdefkzJlXq2D0xuW6ql0WqBM0Vubw", e: "AQAB", }; @@ -32,8 +30,7 @@ Deno.test("RSA - JWK to PKCS8", async () => { Deno.test("RSA - PKCS8 to JWK", async () => { const jwk = { kty: "RSA", - n: - "rlKJ591_fYCKhdflQSNixBhWutUtW5y3l5vFzTxiKE4e9jykJ0Sr7U6GkwjmvplTV7Wgx4zhRr3tYrMqmQ-s_byRK3f2bb-zXF9-fnKGuP7Fp2oYprW3MKxKgNxjRzmx2x7LaV11dHFQv6oigeV2cyY5XB_GnEWUyHY7fCJIJIRdxuskt-77NAU0vrA_ntbWzFFsPP5xWJ8ns_ojTvwu-LT--fpBD3X1nTUR_LzlRgGxGqPHYRCHvY8B2FSPL8ukqfXI3LkvCM77zeR5lwPqIqDFVWcP6TNsOXccqDtBiA3-A6TS3nGmOu3NbZdefkzJlXq2D0xuW6ql0WqBM0Vubw", + n: "rlKJ591_fYCKhdflQSNixBhWutUtW5y3l5vFzTxiKE4e9jykJ0Sr7U6GkwjmvplTV7Wgx4zhRr3tYrMqmQ-s_byRK3f2bb-zXF9-fnKGuP7Fp2oYprW3MKxKgNxjRzmx2x7LaV11dHFQv6oigeV2cyY5XB_GnEWUyHY7fCJIJIRdxuskt-77NAU0vrA_ntbWzFFsPP5xWJ8ns_ojTvwu-LT--fpBD3X1nTUR_LzlRgGxGqPHYRCHvY8B2FSPL8ukqfXI3LkvCM77zeR5lwPqIqDFVWcP6TNsOXccqDtBiA3-A6TS3nGmOu3NbZdefkzJlXq2D0xuW6ql0WqBM0Vubw", e: "AQAB", }; diff --git a/tests/rsa/rsa.import_key.test.ts b/tests/rsa/rsa.import_key.test.ts index 7478c08..7b11fbd 100644 --- a/tests/rsa/rsa.import_key.test.ts +++ b/tests/rsa/rsa.import_key.test.ts @@ -1,7 +1,5 @@ import { RSA } from "./../../rsa.ts"; -import { - assertEquals, -} from "https://deno.land/std@0.63.0/testing/asserts.ts"; +import { assertEquals } from "https://deno.land/std@0.63.0/testing/asserts.ts"; Deno.test("RSA - Import JWK Public Key", () => { const jwk = { diff --git a/tests/rsa/rsa.test.ts b/tests/rsa/rsa.test.ts index 56993d5..1dd7a94 100644 --- a/tests/rsa/rsa.test.ts +++ b/tests/rsa/rsa.test.ts @@ -1,6 +1,4 @@ -import { - assertEquals, -} from "https://deno.land/std@0.63.0/testing/asserts.ts"; +import { assertEquals } from "https://deno.land/std@0.63.0/testing/asserts.ts"; import { RSA } from "./../../mod.ts"; Deno.test("Decrypt RSA OAEP SHA1", async () => { diff --git a/tests/rsa/rsa.verify.test.ts b/tests/rsa/rsa.verify.test.ts index 5d69afa..057ea93 100644 --- a/tests/rsa/rsa.verify.test.ts +++ b/tests/rsa/rsa.verify.test.ts @@ -1,7 +1,5 @@ -import { - assertEquals, -} from "https://deno.land/std@0.63.0/testing/asserts.ts"; -import { RSA, encode } from "./../../mod.ts"; +import { assertEquals } from "https://deno.land/std@0.63.0/testing/asserts.ts"; +import { encode, RSA } from "./../../mod.ts"; Deno.test("Verify RSASSA-PKSC1-v1_5", async () => { const publicKey = `-----BEGIN PUBLIC KEY----- diff --git a/tests/utility/encode.test.ts b/tests/utility/encode.test.ts index f72d8de..ec6586d 100644 --- a/tests/utility/encode.test.ts +++ b/tests/utility/encode.test.ts @@ -1,6 +1,4 @@ -import { - assertEquals, -} from "https://deno.land/std@0.63.0/testing/asserts.ts"; +import { assertEquals } from "https://deno.land/std@0.63.0/testing/asserts.ts"; import { encode } from "./../../mod.ts"; Deno.test("Encoding Utility", () => { From edbd5e2d7a2c1fdc5945ac6e494ff8e372d78594 Mon Sep 17 00:00:00 2001 From: "Visal .In" Date: Sat, 5 Dec 2020 18:27:49 +0700 Subject: [PATCH 4/5] Fixing deno fmt --- src/aes/aes_js.ts | 24 ++++++++---------------- src/rsa/import_key.ts | 10 +++++----- src/rsa/mod.ts | 16 ++++++++-------- src/rsa/rsa_key.ts | 2 +- 4 files changed, 22 insertions(+), 30 deletions(-) diff --git a/src/aes/aes_js.ts b/src/aes/aes_js.ts index 11382ad..17cb2bc 100644 --- a/src/aes/aes_js.ts +++ b/src/aes/aes_js.ts @@ -671,23 +671,19 @@ class AESBlockCiper implements BlockCiper { block[offset + 3], ]; - block[offset] = - xtime(a[0], 0x2) ^ + block[offset] = xtime(a[0], 0x2) ^ xtime(a[1], 0x3) ^ xtime(a[2], 0x1) ^ xtime(a[3], 0x1); - block[offset + 1] = - xtime(a[0], 0x1) ^ + block[offset + 1] = xtime(a[0], 0x1) ^ xtime(a[1], 0x2) ^ xtime(a[2], 0x3) ^ xtime(a[3], 0x1); - block[offset + 2] = - xtime(a[0], 0x1) ^ + block[offset + 2] = xtime(a[0], 0x1) ^ xtime(a[1], 0x1) ^ xtime(a[2], 0x2) ^ xtime(a[3], 0x3); - block[offset + 3] = - xtime(a[0], 0x3) ^ + block[offset + 3] = xtime(a[0], 0x3) ^ xtime(a[1], 0x1) ^ xtime(a[2], 0x1) ^ xtime(a[3], 0x2); @@ -704,23 +700,19 @@ class AESBlockCiper implements BlockCiper { block[offset + 3], ]; - block[offset] = - xtime(a[0], 0xe) ^ + block[offset] = xtime(a[0], 0xe) ^ xtime(a[1], 0xb) ^ xtime(a[2], 0xd) ^ xtime(a[3], 0x9); - block[offset + 1] = - xtime(a[0], 0x9) ^ + block[offset + 1] = xtime(a[0], 0x9) ^ xtime(a[1], 0xe) ^ xtime(a[2], 0xb) ^ xtime(a[3], 0xd); - block[offset + 2] = - xtime(a[0], 0xd) ^ + block[offset + 2] = xtime(a[0], 0xd) ^ xtime(a[1], 0x9) ^ xtime(a[2], 0xe) ^ xtime(a[3], 0xb); - block[offset + 3] = - xtime(a[0], 0xb) ^ + block[offset + 3] = xtime(a[0], 0xb) ^ xtime(a[1], 0xd) ^ xtime(a[2], 0x9) ^ xtime(a[3], 0xe); diff --git a/src/rsa/import_key.ts b/src/rsa/import_key.ts index 25e93e6..a8078c2 100644 --- a/src/rsa/import_key.ts +++ b/src/rsa/import_key.ts @@ -7,7 +7,7 @@ import { os2ip } from "./primitives.ts"; type RSAImportKeyFormat = "auto" | "jwk" | "pem"; type RSAPublicKeyFormat = [[string, null], [[bigint, bigint]]]; type RSACertKeyFormat = [ - [number, string, null, null, null, RSAPublicKeyFormat] + [number, string, null, null, null, RSAPublicKeyFormat], ]; /** @@ -59,7 +59,7 @@ function rsa_import_jwk(key: JSONWebKey): RSAKeyParams { function rsa_import_pem_cert(key: string): RSAKeyParams { const trimmedKey = key.substr(27, key.length - 53); const parseKey = ber_simple( - ber_decode(base64_to_binary(trimmedKey)) + ber_decode(base64_to_binary(trimmedKey)), ) as RSACertKeyFormat; return { @@ -78,7 +78,7 @@ function rsa_import_pem_cert(key: string): RSAKeyParams { function rsa_import_pem_private(key: string): RSAKeyParams { const trimmedKey = key.substr(31, key.length - 61); const parseKey = ber_simple( - ber_decode(base64_to_binary(trimmedKey)) + ber_decode(base64_to_binary(trimmedKey)), ) as bigint[]; return { @@ -103,7 +103,7 @@ function rsa_import_pem_private(key: string): RSAKeyParams { function rsa_import_pem_public(key: string): RSAKeyParams { const trimmedKey = key.substr(26, key.length - 51); const parseKey = ber_simple( - ber_decode(base64_to_binary(trimmedKey)) + ber_decode(base64_to_binary(trimmedKey)), ) as RSAPublicKeyFormat; return { @@ -143,7 +143,7 @@ function rsa_import_pem(key: string): RSAKeyParams { */ export function rsa_import_key( key: string | JSONWebKey, - format: RSAImportKeyFormat + format: RSAImportKeyFormat, ): RSAKeyParams { const finalFormat = format === "auto" ? detect_format(key) : format; diff --git a/src/rsa/mod.ts b/src/rsa/mod.ts index 6e346e4..7834943 100644 --- a/src/rsa/mod.ts +++ b/src/rsa/mod.ts @@ -1,4 +1,4 @@ -import type { RSAOption, RSASignOption, JSONWebKey } from "./common.ts"; +import type { JSONWebKey, RSAOption, RSASignOption } from "./common.ts"; import { WebCryptoRSA } from "./rsa_wc.ts"; import { PureRSA } from "./rsa_js.ts"; import { RawBinary } from "../binary.ts"; @@ -34,7 +34,7 @@ export class RSA { : PureRSA.encrypt; return new RawBinary( - await func(this.key, computeMessage(m), computedOption) + await func(this.key, computeMessage(m), computedOption), ); } @@ -51,7 +51,7 @@ export class RSA { async verify( signature: Uint8Array, message: Uint8Array | string, - options?: Partial + options?: Partial, ): Promise { const computedOption: RSASignOption = { ...options, @@ -63,13 +63,13 @@ export class RSA { this.key, signature, computeMessage(message), - computedOption + computedOption, ); } async sign( message: Uint8Array | string, - options?: Partial + options?: Partial, ): Promise { const computedOption: RSASignOption = { ...options, @@ -80,13 +80,13 @@ export class RSA { return await PureRSA.sign( this.key, computeMessage(message), - computedOption + computedOption, ); } static parseKey( key: string | JSONWebKey, - format: "auto" | "jwk" | "pem" = "auto" + format: "auto" | "jwk" | "pem" = "auto", ): RSAKey { return this.importKey(key, format); } @@ -99,7 +99,7 @@ export class RSA { */ static importKey( key: string | JSONWebKey, - format: "auto" | "jwk" | "pem" = "auto" + format: "auto" | "jwk" | "pem" = "auto", ): RSAKey { return new RSAKey(rsa_import_key(key, format)); } diff --git a/src/rsa/rsa_key.ts b/src/rsa/rsa_key.ts index 1ff8559..7bd0956 100644 --- a/src/rsa/rsa_key.ts +++ b/src/rsa/rsa_key.ts @@ -1,4 +1,4 @@ -import type { RSAKeyParams, JSONWebKey } from "./common.ts"; +import type { JSONWebKey, RSAKeyParams } from "./common.ts"; import { encode } from "./../../src/utility/encode.ts"; import { rsa_export_pkcs8_private, From 8b403da7c7152bf090513e19ffe5736a0c9c28fa Mon Sep 17 00:00:00 2001 From: "Visal .In" Date: Sat, 5 Dec 2020 18:28:49 +0700 Subject: [PATCH 5/5] Dump the test to use deno 1.4.0 --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0c8fec3..78bfd61 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,7 +10,7 @@ jobs: - name: Setup deno uses: denolib/setup-deno@master with: - deno-version: 1.2.0 + deno-version: 1.4.0 - name: check fmt run: deno fmt --check - name: Run tests