Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RSA Extension #1699

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions extensions/Themadpunter/rsa.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Name: RSA
// ID: themadpunter-rsa
// Description: Encrypt text and files using RSA, the world's best asymmetric encryption algorithm.
// By: Themadpunter <https://scratch.mit.edu/users/The_Mad_Punter/>
// License: MIT

(function(Scratch) {
'use strict';

// Load the JSEncrypt library from a CDN
const script = document.createElement('script');
script.src = 'https://cdn.jsdelivr.net/npm/jsencrypt/bin/jsencrypt.min.js';
document.head.appendChild(script);

class RSAExtension {
getInfo() {
return {
id: 'themadpunter_rsa',
name: 'TurboRSA',
color1: '#FF0000', // Primary color (red)
color2: '#CC0000', // Secondary color (darker red)
blocks: [
{
opcode: 'generateKeys',
blockType: Scratch.BlockType.REPORTER,
text: 'Generate RSA keys',
arguments: {}
},
{
opcode: 'encrypt',
blockType: Scratch.BlockType.REPORTER,
text: 'Encrypt [TEXT] with public key [KEY]',
arguments: {
TEXT: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'Hello, world!'
},
KEY: {
type: Scratch.ArgumentType.STRING,
defaultValue: ''
}
}
},
{
opcode: 'decrypt',
blockType: Scratch.BlockType.REPORTER,
text: 'Decrypt [TEXT] with private key [KEY]',
arguments: {
TEXT: {
type: Scratch.ArgumentType.STRING,
defaultValue: ''
},
KEY: {
type: Scratch.ArgumentType.STRING,
defaultValue: ''
}
}
}
]
};
}

generateKeys() {
const crypt = new JSEncrypt({ default_key_size: 1024 });

Check failure on line 64 in extensions/Themadpunter/rsa.js

View workflow job for this annotation

GitHub Actions / lint

'JSEncrypt' is not defined
const publicKey = crypt.getPublicKeyB64();
const privateKey = crypt.getPrivateKeyB64();
return JSON.stringify({ publicKey, privateKey });
}

encrypt(args) {
const crypt = new JSEncrypt();

Check failure on line 71 in extensions/Themadpunter/rsa.js

View workflow job for this annotation

GitHub Actions / lint

'JSEncrypt' is not defined
crypt.setPublicKey(args.KEY);
const encrypted = crypt.encrypt(args.TEXT);
return encrypted ? encrypted : 'Encryption failed';
}

decrypt(args) {
const crypt = new JSEncrypt();

Check failure on line 78 in extensions/Themadpunter/rsa.js

View workflow job for this annotation

GitHub Actions / lint

'JSEncrypt' is not defined
crypt.setPrivateKey(args.KEY);
const decrypted = crypt.decrypt(args.TEXT);
return decrypted ? decrypted : 'Decryption failed';
}
}

// Wait for the script to load before registering the extension
script.onload = () => {
Scratch.extensions.register(new RSAExtension());
};
})(Scratch);
Loading