Skip to content

Commit e483675

Browse files
committed
Add SHA512
1 parent 4e5f437 commit e483675

File tree

7 files changed

+29
-24
lines changed

7 files changed

+29
-24
lines changed

src/hash.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
import { sha1 } from "https://denopkg.com/chiefbiiko/sha1@v1.0.3/mod.ts";
22
import { sha256 } from "https://denopkg.com/chiefbiiko/sha256@v1.0.2/mod.ts";
3+
import { sha512 } from "https://denopkg.com/chiefbiiko/sha512/mod.ts";
4+
import { RSAHashAlgorithm } from "./rsa/common.ts";
35

46
export function digest(
5-
algorithm: "sha1" | "sha256",
7+
algorithm: RSAHashAlgorithm,
68
m: Uint8Array,
79
): Uint8Array {
810
if (algorithm === "sha1") {
911
return sha1(m) as Uint8Array;
1012
} else if (algorithm === "sha256") {
1113
return sha256(m) as Uint8Array;
14+
} else if (algorithm === "sha512") {
15+
return sha512(m) as Uint8Array;
1216
}
1317

1418
throw "Unsupport hash algorithm";
1519
}
1620

17-
export function digestLength(algorithm: "sha1" | "sha256") {
21+
export function digestLength(algorithm: RSAHashAlgorithm) {
22+
if (algorithm === "sha512") return 64;
1823
if (algorithm === "sha256") return 32;
24+
1925
return 20;
2026
}

src/rsa/common.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export type RSAHashAlgorithm = "sha1" | "sha256" | "sha512";
2+
13
export interface RSAKeyParams {
24
n: bigint;
35
e?: bigint;
@@ -11,12 +13,12 @@ export interface RSAKeyParams {
1113
}
1214

1315
export interface RSAOption {
14-
hash: "sha1" | "sha256";
16+
hash: RSAHashAlgorithm;
1517
padding: "oaep" | "pkcs1";
1618
}
1719

1820
export interface RSASignOption {
19-
hash: "sha256";
21+
hash: RSAHashAlgorithm;
2022
algorithm: "rsassa-pkcs1-v1_5" | "rsassa-pss";
2123
}
2224

src/rsa/eme_oaep.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { digest } from "./../hash.ts";
22
import { mgf1 } from "./primitives.ts";
33
import { concat, random_bytes, xor } from "./../helper.ts";
4+
import { RSAHashAlgorithm } from "./common.ts";
45

56
/**
67
* https://tools.ietf.org/html/rfc3447#page-10
@@ -14,7 +15,7 @@ export function eme_oaep_encode(
1415
label: Uint8Array,
1516
m: Uint8Array,
1617
k: number,
17-
algorithm: "sha1" | "sha256",
18+
algorithm: RSAHashAlgorithm,
1819
): Uint8Array {
1920
const labelHash = new Uint8Array(digest(algorithm, label));
2021
const ps = new Uint8Array(k - labelHash.length * 2 - 2 - m.length);
@@ -32,7 +33,7 @@ export function eme_oaep_decode(
3233
label: Uint8Array,
3334
c: Uint8Array,
3435
k: number,
35-
algorithm: "sha1" | "sha256",
36+
algorithm: RSAHashAlgorithm,
3637
): Uint8Array {
3738
const labelHash = new Uint8Array(digest(algorithm, label));
3839
const maskedSeed = c.slice(1, 1 + labelHash.length);

src/rsa/emsa_pss.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { digest } from "../hash.ts";
22
import { xor } from "../helper.ts";
3+
import { RSAHashAlgorithm } from "./common.ts";
34
import { mgf1 } from "./primitives.ts";
45

56
export function emsa_pss_encode(
67
m: Uint8Array,
78
emBits: number,
89
sLen: number,
9-
algorithm: "sha1" | "sha256",
10+
algorithm: RSAHashAlgorithm,
1011
) {
1112
const mHash = digest(algorithm, m);
1213
const hLen = mHash.length;
@@ -39,7 +40,7 @@ export function emsa_pss_verify(
3940
em: Uint8Array,
4041
emBits: number,
4142
sLen: number,
42-
algorithm: "sha1" | "sha256",
43+
algorithm: RSAHashAlgorithm,
4344
): boolean {
4445
const mHash = digest(algorithm, m);
4546
const hLen = mHash.length;

src/rsa/primitives.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { digest } from "./../hash.ts";
2-
3-
type HashFunction = (b: Uint8Array) => Uint8Array;
4-
type HashAlgorithm = "sha1" | "sha256";
2+
import { RSAHashAlgorithm } from "./common.ts";
53

64
/**
75
* I2OSP converts a nonnegative integer to an octet string of a specified length.
@@ -36,22 +34,17 @@ export function os2ip(m: Uint8Array): bigint {
3634
export function mgf1(
3735
seed: Uint8Array,
3836
length: number,
39-
hash: HashFunction | HashAlgorithm,
37+
hash: RSAHashAlgorithm,
4038
): Uint8Array {
4139
let counter = 0n;
4240
let output: number[] = [];
4341

4442
while (output.length < length) {
45-
let h;
4643
const c = i2osp(counter, 4);
4744

48-
if (typeof hash === "function") {
49-
h = hash(new Uint8Array([...seed, ...c]));
50-
} else {
51-
h = new Uint8Array(
52-
digest(hash, new Uint8Array([...seed, ...c])),
53-
);
54-
}
45+
const h = new Uint8Array(
46+
digest(hash, new Uint8Array([...seed, ...c])),
47+
);
5548

5649
output = [...output, ...h];
5750
counter++;

src/rsa/rsa_internal.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { concat, random_bytes } from "./../helper.ts";
55
import { ber_decode, ber_simple } from "./basic_encoding_rule.ts";
66
import { RawBinary } from "../binary.ts";
77
import { RSAKey } from "./rsa_key.ts";
8+
import { RSAHashAlgorithm } from "./common.ts";
89

910
/**
1011
* @param n public key modulus
@@ -46,7 +47,7 @@ export function rsa_oaep_encrypt(
4647
n: bigint,
4748
e: bigint,
4849
m: Uint8Array,
49-
algorithm: "sha1" | "sha256",
50+
algorithm: RSAHashAlgorithm,
5051
) {
5152
const em = eme_oaep_encode(new Uint8Array(0), m, bytes, algorithm);
5253
const msg = os2ip(em);
@@ -57,7 +58,7 @@ export function rsa_oaep_encrypt(
5758
export function rsa_oaep_decrypt(
5859
key: RSAKey,
5960
c: Uint8Array,
60-
algorithm: "sha1" | "sha256",
61+
algorithm: RSAHashAlgorithm,
6162
) {
6263
const em = rsadp(key, os2ip(c));
6364
const m = eme_oaep_decode(

src/rsa/rsassa_pss.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { RawBinary } from "../binary.ts";
22
import { digestLength } from "../hash.ts";
3+
import { RSAHashAlgorithm } from "./common.ts";
34
import { emsa_pss_encode, emsa_pss_verify } from "./emsa_pss.ts";
45
import { i2osp, os2ip } from "./primitives.ts";
56
import { rsaep } from "./rsa_internal.ts";
@@ -8,7 +9,7 @@ import { RSAKey } from "./rsa_key.ts";
89
export function rsassa_pss_sign(
910
key: RSAKey,
1011
m: Uint8Array,
11-
algorithm: "sha256",
12+
algorithm: RSAHashAlgorithm,
1213
): RawBinary {
1314
if (!key.d) throw "Invalid RSA Key";
1415

@@ -21,7 +22,7 @@ export function rsassa_pss_verify(
2122
key: RSAKey,
2223
m: Uint8Array,
2324
signature: Uint8Array,
24-
algorithm: "sha256",
25+
algorithm: RSAHashAlgorithm,
2526
): boolean {
2627
if (!key.e) throw "Invalid RSA Key";
2728

0 commit comments

Comments
 (0)