Skip to content

Commit 7e943e6

Browse files
added several options and algorithms
1 parent 840a8ae commit 7e943e6

File tree

5 files changed

+947
-158
lines changed

5 files changed

+947
-158
lines changed

edition.js

+31-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export class Cryptography extends Formulae.EditionPackage {};
2323
Cryptography.setEditions = function() {
2424
[
2525
"GenerateAsymmetricKeysForEncryption",
26+
"GenerateSymmetricKeyForEncryption",
2627
"GenerateAsymmetricKeysForSigning"
2728
].forEach(
2829
tag => Formulae.addEdition(
@@ -33,6 +34,8 @@ Cryptography.setEditions = function() {
3334
)
3435
);
3536

37+
// Operations
38+
3639
[
3740
[ "Encryption", "Encrypt", 2 ],
3841
[ "Encryption", "Decrypt", 2 ],
@@ -48,11 +51,35 @@ Cryptography.setEditions = function() {
4851
)
4952
);
5053

51-
[ "SHA-1", "SHA-256", "SHA-384", "SHA-512" ].forEach(tag => Formulae.addEdition(
52-
"Cryptography.Hashing.Algorithm",
54+
Formulae.addEdition(
55+
this.messages.pathRandom,
56+
null,
57+
this.messages.leafRandom,
58+
() => Expression.multipleEdition("Cryptography.Random", 1, 0)
59+
);
60+
61+
// Algorithms
62+
63+
[
64+
[ "Hashing", "Hashing", "SHA-1" ],
65+
[ "Hashing", "Hashing", "SHA-256" ],
66+
[ "Hashing", "Hashing", "SHA-384" ],
67+
[ "Hashing", "Hashing", "SHA-512" ],
68+
[ "Asymmetric encryption", "AsymmetricEncryption", "RSA-OAEP" ],
69+
[ "SymmetricEncryption", "SymmetricEncryption", "AES-CTR" ],
70+
[ "SymmetricEncryption", "SymmetricEncryption", "AES-CBC" ],
71+
[ "SymmetricEncryption", "SymmetricEncryption", "AES-GCM" ],
72+
[ "Signing", "Signing", "RSASSA-PKCS1-v1_5" ],
73+
[ "Signing", "Signing", "RSA-PSS" ],
74+
[ "Signing", "Signing", "ECDSA" ],
75+
[ "Signing", "Signing", "HMAC" ],
76+
77+
].forEach(
78+
row => Formulae.addEdition(
79+
"Cryptography.Algorithm." + row[0],
5380
null,
54-
tag,
55-
() => Expression.replacingEdition("Cryptography.Hashing.Algorithm." + tag)
81+
row[2],
82+
() => Expression.replacingEdition("Cryptography.Algorithm." + row[1] + "." + row[2])
5683
));
5784
};
5885

expression.js

+101-40
Original file line numberDiff line numberDiff line change
@@ -86,24 +86,39 @@ Cryptography.Key = class extends Expression.Literal {
8686
*/
8787

8888
switch (this.key.algorithm.name) {
89-
case "RSA-OAEP":
90-
case "RSASSA-PKCS1-v1_5": {
91-
let arrayBuffer;
92-
93-
if (this.key.type === "private") {
94-
arrayBuffer = await window.crypto.subtle.exportKey("pkcs8", this.key);
89+
case "RSA-OAEP": // encryption
90+
case "RSASSA-PKCS1-v1_5": // signing
91+
case "RSA-PSS": // signing
92+
{
93+
let arrayBuffer;
94+
95+
if (this.key.type === "private") {
96+
arrayBuffer = await window.crypto.subtle.exportKey("pkcs8", this.key);
97+
}
98+
else { // public
99+
arrayBuffer = await window.crypto.subtle.exportKey("spki", this.key);
100+
}
101+
102+
return [
103+
Utils.bytesToBase64(new Uint8Array(arrayBuffer)), // value
104+
this.key.type, // type
105+
this.key.algorithm.name, // algorithm
106+
this.key.algorithm.hash.name // parameter
107+
];
95108
}
96-
else {
97-
arrayBuffer = await window.crypto.subtle.exportKey("spki", this.key);
109+
110+
case "AES-CTR":
111+
case "AES-CBC":
112+
case "AES-GCM":
113+
{
114+
let arrayBuffer = await window.crypto.subtle.exportKey("raw", this.key);
115+
return [
116+
Utils.bytesToBase64(new Uint8Array(arrayBuffer)), // value
117+
this.key.type, // type
118+
this.key.algorithm.name, // algorithm
119+
this.key.algorithm.length.toString() // parameter
120+
];
98121
}
99-
100-
return [
101-
Utils.bytesToBase64(new Uint8Array(arrayBuffer)), // value
102-
this.key.type, // type
103-
this.key.algorithm.name, // algorithm
104-
this.key.algorithm.hash.name // parameter
105-
];
106-
}
107122
}
108123
}
109124

@@ -143,21 +158,35 @@ Cryptography.Key = class extends Expression.Literal {
143158
*/
144159

145160
switch (algorithmName) {
146-
case "RSA-OAEP":
147-
case "RSASSA-PKCS1-v1_5": {
148-
if (type == "private") {
149-
format = "pkcs8";
150-
keyData = Utils.base64ToBytes(strings[0]);
151-
algorithm = { name: algorithmName, hash: parameter };
152-
keyUsages = [ algorithmName == "RSA-OAEP" ? "decrypt" : "sign" ];
161+
case "RSA-OAEP": // encryption
162+
case "RSASSA-PKCS1-v1_5": // signing
163+
case "RSA-PSS": // signing
164+
{
165+
if (type == "private") {
166+
format = "pkcs8";
167+
keyData = Utils.base64ToBytes(strings[0]);
168+
algorithm = { name: algorithmName, hash: parameter };
169+
keyUsages = [ algorithmName == "RSA-OAEP" ? "decrypt" : "sign" ];
170+
}
171+
else { // public
172+
format = "spki";
173+
keyData = Utils.base64ToBytes(strings[0]);
174+
algorithm = { name: algorithmName, hash: parameter };
175+
keyUsages = [ algorithmName == "RSA-OAEP" ? "encrypt" : "verify" ];
176+
}
153177
}
154-
else {
155-
format = "spki";
178+
break;
179+
180+
case "AES-CTR":
181+
case "AES-CBC":
182+
case "AES-GCM":
183+
{
184+
format = "raw";
156185
keyData = Utils.base64ToBytes(strings[0]);
157-
algorithm = { name: algorithmName, hash: parameter };
158-
keyUsages = [ algorithmName == "RSA-OAEP" ? "encrypt" : "verify" ];
186+
algorithm = { name: algorithmName, length: Number(parameter) };
187+
keyUsages = [ "encrypt", "decrypt" ];
159188
}
160-
}
189+
break;
161190
}
162191

163192
let promise = window.crypto.subtle.importKey(
@@ -174,7 +203,7 @@ Cryptography.Key = class extends Expression.Literal {
174203
}
175204

176205
getLiteral() {
177-
return "<" + this.key.type + " - " + this.key.usages + ">";
206+
return "<" + this.key.type + " - " + this.key.usages.join(", ") + ">";
178207
}
179208
}
180209

@@ -191,14 +220,17 @@ Cryptography.setExpressions = function(module) {
191220
)
192221
);
193222

223+
// Operations
224+
194225
[
195-
[ "Key", "GenerateAsymmetricKeysForEncryption", 1, 1 ],
196-
[ "Key", "GenerateAsymmetricKeysForSigning", 1, 1 ],
197-
[ "Encryption", "Encrypt", 2, 2 ],
198-
[ "Encryption", "Decrypt", 2, 2 ],
226+
[ "Key", "GenerateAsymmetricKeysForEncryption", 1, 2 ],
227+
[ "Key", "GenerateSymmetricKeyForEncryption", 1, 2 ],
228+
[ "Key", "GenerateAsymmetricKeysForSigning", 1, 2 ],
229+
[ "Encryption", "Encrypt", 2, 3 ],
230+
[ "Encryption", "Decrypt", 2, 3 ],
199231
[ "Hashing", "Hash", 2, 2 ],
200-
[ "Signing", "Sign", 2, 2 ],
201-
[ "Signing", "Verify", 3, 3 ],
232+
[ "Signing", "Sign", 2, 3 ],
233+
[ "Signing", "Verify", 3, 4 ],
202234
].forEach(
203235
row => Formulae.setExpression(
204236
module,
@@ -215,14 +247,43 @@ Cryptography.setExpressions = function(module) {
215247
)
216248
);
217249

218-
[ "SHA-1", "SHA-256", "SHA-384", "SHA-512" ].forEach(
219-
tag => Formulae.setExpression(
250+
Formulae.setExpression(
251+
module,
252+
"Cryptography.Random",
253+
{
254+
clazz: Expression.Function,
255+
getTag: () => "Cryptography.Random",
256+
getMnemonic: () => Cryptography.messages.mnemonicRandom,
257+
getName: () => Cryptography.messages.nameRandom,
258+
getChildName: index => Cryptography.messages.childRandom,
259+
min: 1,
260+
max: 1
261+
}
262+
);
263+
264+
// Algorithms
265+
266+
[
267+
[ "Hashing", "SHA-1" ],
268+
[ "Hashing", "SHA-256" ],
269+
[ "Hashing", "SHA-384" ],
270+
[ "Hashing", "SHA-512" ],
271+
[ "AsymmetricEncryption", "RSA-OAEP" ],
272+
[ "SymmetricEncryption", "AES-CTR" ],
273+
[ "SymmetricEncryption", "AES-CBC" ],
274+
[ "SymmetricEncryption", "AES-GCM" ],
275+
[ "Signing", "RSASSA-PKCS1-v1_5" ],
276+
[ "Signing", "RSA-PSS" ],
277+
[ "Signing", "ECDSA" ],
278+
[ "Signing", "HMAC" ],
279+
].forEach(
280+
row => Formulae.setExpression(
220281
module,
221-
"Cryptography.Hashing.Algorithm." + tag,
282+
"Cryptography.Algorithm." + row[0] + "." + row[1],
222283
{
223284
clazz: Expression.LabelExpression,
224-
getTag: () => "Cryptography.Hashing.Algorithm." + tag,
225-
getLabel: () => tag,
285+
getTag: () => "Cryptography.Algorithm." + row[0] + "." + row[1],
286+
getLabel: () => row[1],
226287
getName: () => "yyy"
227288
}
228289
)

i18n/messages.json

+22-8
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,29 @@
99

1010
"nameGenerateAsymmetricKeysForEncryption" : "Generate asymmetric keys for encryption",
1111
"mnemonicGenerateAsymmetricKeysForEncryption" : "GenerateAsymmetricKeysForEncryption",
12-
"childrenGenerateAsymmetricKeysForEncryption" : "Algorithm",
12+
"childrenGenerateAsymmetricKeysForEncryption" : [ "Algorithm", "Options" ],
1313
"leafGenerateAsymmetricKeysForEncryption" : "Generate asymmetric keys for encryption",
1414

15+
"nameGenerateSymmetricKeyForEncryption" : "Generate symmetric key for encryption",
16+
"mnemonicGenerateSymmetricKeyForEncryption" : "GenerateSymmetricKeyForEncryption",
17+
"childrenGenerateSymmetricKeyForEncryption" : [ "Algorithm", "Options" ],
18+
"leafGenerateSymmetricKeyForEncryption" : "Generate symmetric key for encryption",
19+
1520
"nameGenerateAsymmetricKeysForSigning" : "Generate asymmetric keys for signing",
1621
"mnemonicGenerateAsymmetricKeysForSigning" : "GenerateAsymmetricKeysForSigning",
17-
"childrenGenerateAsymmetricKeysForSigning" : "Algorithm",
18-
"leafGenerateAsymmetricKeysForSigning" : "Generate asymmetric keys for signing",
22+
"childrenGenerateAsymmetricKeysForSigning" : [ "Algorithm", "Options" ],
23+
"leafGenerateAsymmetricKeysForSigning" : "Generate keys for signing",
1924

2025
"pathEncryption" : "Cryptography.Encryption",
2126

2227
"nameEncrypt" : "Encrypt",
2328
"mnemonicEncrypt" : "Encrypt",
24-
"childrenEncrypt" : [ "Plain", "Key" ],
29+
"childrenEncrypt" : [ "Plain", "Key", "Options" ],
2530
"leafEncrypt" : "Encrypt",
2631

2732
"nameDecrypt" : "Decrypt",
2833
"mnemonicDecrypt" : "Decrypt",
29-
"childrenDecrypt" : [ "Cipher", "Key" ],
34+
"childrenDecrypt" : [ "Cipher", "Key", "Options" ],
3035
"leafDecrypt" : "Decrypt",
3136

3237
"pathHashing" : "Cryptography.Hashing",
@@ -40,11 +45,20 @@
4045

4146
"nameSign" : "Sign",
4247
"mnemonicSign" : "Sign",
43-
"childrenSign" : [ "Data", "Key" ],
48+
"childrenSign" : [ "Data", "Key", "Options" ],
4449
"leafSign" : "Sign",
4550

4651
"nameVerify" : "Verify signature",
4752
"mnemonicVerify" : "VerifySignature",
48-
"childrenVerify" : [ "Data", "Signature", "Key" ],
49-
"leafVerify" : "Verify signature"
53+
"childrenVerify" : [ "Data", "Signature", "Key", "Options" ],
54+
"leafVerify" : "Verify signature",
55+
56+
"pathRandom" : "Cryptography.Random number generation",
57+
58+
"nameRandom" : "Random",
59+
"mnemonicRandom" : "Random",
60+
"childRandom" : "Number of bytes",
61+
"leafRandom" : "Random",
62+
63+
"pathAlgorithm" : "Cryptography.Algorithm"
5064
}

i18n/messages_es.json

+22-8
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,29 @@
99

1010
"nameGenerateAsymmetricKeysForEncryption" : "Generar llaves asimétricas para encriptación",
1111
"mnemonicGenerateAsymmetricKeysForEncryption" : "GenerarLlavesAsimétricasParaEncriptación",
12-
"childrenGenerateAsymmetricKeysForEncryption" : "Algoritmo",
12+
"childrenGenerateAsymmetricKeysForEncryption" : [ "Algoritmo", "Opciones" ],
1313
"leafGenerateAsymmetricKeysForEncryption" : "Generar llaves asimétricas para encriptación",
1414

15+
"nameGenerateSymmetricKeyForEncryption" : "Generar llave simétric para encriptación",
16+
"mnemonicGenerateSymmetricKeyForEncryption" : "GenerarLlaveSimétricaParaEncriptación",
17+
"childrenGenerateSymmetricKeyForEncryption" : [ "Algoritmo", "Opciones" ],
18+
"leafGenerateSymmetricKeyForEncryption" : "Generar llave simétrica para encriptación",
19+
1520
"nameGenerateAsymmetricKeysForSigning" : "Generar llaves asimétricas para firmado",
1621
"mnemonicGenerateAsymmetricKeysForSigning" : "GenerarLlavesAsimétricasParaFirmado",
17-
"childrenGenerateAsymmetricKeysForSigning" : "Algoritmo",
18-
"leafGenerateAsymmetricKeysForSigning" : "Generar llaves asimétricas para firmado",
22+
"childrenGenerateAsymmetricKeysForSigning" : [ "Algoritmo", "Opciones" ],
23+
"leafGenerateAsymmetricKeysForSigning" : "Generar llaves para firmado",
1924

2025
"pathEncryption" : "Criptografía.Encriptación",
2126

2227
"nameEncrypt" : "Encriptar",
2328
"mnemonicEncrypt" : "Encriptar",
24-
"childrenEncrypt" : [ "Datos a encriptar", "Llave" ],
29+
"childrenEncrypt" : [ "Datos a encriptar", "Llave", "Opciones" ],
2530
"leafEncrypt" : "Encriptar",
2631

2732
"nameDecrypt" : "Desencriptar",
2833
"mnemonicDecrypt" : "Desencriptar",
29-
"childrenDecrypt" : [ "Datos encriptados", "Llave" ],
34+
"childrenDecrypt" : [ "Datos encriptados", "Llave", "Opciones" ],
3035
"leafDecrypt" : "Desencriptar",
3136

3237
"pathHashing" : "Criptografía.Hashing",
@@ -40,11 +45,20 @@
4045

4146
"nameSign" : "Firmar",
4247
"mnemonicSign" : "Firmar",
43-
"childrenSign" : [ "Datos", "Llave" ],
48+
"childrenSign" : [ "Datos", "Llave", "Opciones" ],
4449
"leafSign" : "Firmar",
4550

4651
"nameVerify" : "Verificar firma",
4752
"mnemonicVerify" : "VerificarFirma",
48-
"childrenVerify" : [ "Datos", "Firma", "Llave" ],
49-
"leafVerify" : "Verificar firma"
53+
"childrenVerify" : [ "Datos", "Firma", "Llave", "Opciones" ],
54+
"leafVerify" : "Verificar firma",
55+
56+
"pathRandom" : "Cryptografía.Generación de números aleatorios",
57+
58+
"nameRandom" : "Aleatorio",
59+
"mnemonicRandom" : "Aleatorio",
60+
"childRandom" : "Número de bytes",
61+
"leafRandom" : "Aleatorio",
62+
63+
"pathAlgorithm" : "Cryptografía.Algoritmos"
5064
}

0 commit comments

Comments
 (0)