- 
                Notifications
    You must be signed in to change notification settings 
- Fork 295
New Extension: SK17 #2206
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
          
     Open
      
      
            SkyBuilder1717
  wants to merge
  14
  commits into
  TurboWarp:master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
SkyBuilder1717:master
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    New Extension: SK17 #2206
Changes from all commits
      Commits
    
    
            Show all changes
          
          
            14 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      165204c
              
                New Extension
              
              
                SkyBuilder1717 b65a01b
              
                [Automated] Format code
              
              
                DangoCat ae3795e
              
                Text to Path
              
              
                SkyBuilder1717 a27f80f
              
                Merge branch 'master' of https://github.com/SkyBuilder1717/extensions
              
              
                SkyBuilder1717 4c62ea2
              
                'use strict'; Scratch.translate; await;
              
              
                SkyBuilder1717 2d81dff
              
                Fix async function (Scratch)
              
              
                SkyBuilder1717 f525d8c
              
                [Automated] Format code
              
              
                DangoCat e60c75f
              
                Merge branch 'TurboWarp:master' into master
              
              
                SkyBuilder1717 572c01f
              
                Update SK17.js
              
              
                SkyBuilder1717 1598145
              
                Scratch.translate
              
              
                SkyBuilder1717 d95b611
              
                [Automated] Format code
              
              
                DangoCat f7ac819
              
                Add files via upload
              
              
                SkyBuilder1717 7052509
              
                Update extensions/SkyBuilder1717/SK17.js
              
              
                SkyBuilder1717 82bfe86
              
                Merge branch 'TurboWarp:master' into master
              
              
                SkyBuilder1717 File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,284 @@ | ||
| // Name: Encryption | ||
| // ID: SkyBuilder1717Encryption | ||
| // Description: Adds new encryption format: SK17. Useful for saving games and loading private data in compiled games. | ||
| // By: SkyBuilder1717 | ||
| // License: MIT | ||
|  | ||
| (function (Scratch) { | ||
| "use strict"; | ||
|  | ||
| if (!Scratch.extensions.unsandboxed) | ||
| throw new Error("SK17 must run unsandboxed!"); | ||
|  | ||
| const SIG = new Uint8Array([83, 75, 49, 55]); | ||
|  | ||
| function bytesToLatin1String(u8) { | ||
| const CHUNK = 0x8000; | ||
| let result = ""; | ||
| for (let i = 0; i < u8.length; i += CHUNK) { | ||
| const slice = u8.subarray(i, i + CHUNK); | ||
| result += String.fromCharCode.apply(null, slice); | ||
| } | ||
| return result; | ||
| } | ||
|  | ||
| function latin1StringToBytes(str) { | ||
| const u8 = new Uint8Array(str.length); | ||
| for (let i = 0; i < str.length; i++) { | ||
| u8[i] = str.charCodeAt(i) & 0xff; | ||
| } | ||
| return u8; | ||
| } | ||
|  | ||
| function concat(...parts) { | ||
| const total = parts.reduce((s, p) => s + p.length, 0); | ||
| const out = new Uint8Array(total); | ||
| let off = 0; | ||
| for (const p of parts) { | ||
| out.set(p, off); | ||
| off += p.length; | ||
| } | ||
| return out; | ||
| } | ||
|  | ||
| async function deriveKey(password, salt) { | ||
| const enc = new TextEncoder(); | ||
| const pwKey = await crypto.subtle.importKey( | ||
| "raw", | ||
| enc.encode(password), | ||
| { name: "PBKDF2" }, | ||
| false, | ||
| ["deriveKey"] | ||
| ); | ||
| return await crypto.subtle.deriveKey( | ||
| { | ||
| name: "PBKDF2", | ||
| salt: salt, | ||
| iterations: 150000, | ||
| hash: "SHA-256", | ||
| }, | ||
| pwKey, | ||
| { name: "AES-GCM", length: 256 }, | ||
| false, | ||
| ["encrypt", "decrypt"] | ||
| ); | ||
| } | ||
|  | ||
| function xorEncrypt(data, password) { | ||
| const pwdBytes = new TextEncoder().encode(password); | ||
| const result = new Uint8Array(data.length); | ||
| for (let i = 0; i < data.length; i++) { | ||
| result[i] = data[i] ^ pwdBytes[i % pwdBytes.length]; | ||
| } | ||
| return result; | ||
| } | ||
|  | ||
| function rotateBytes(data, _) { | ||
| const len = data.length; | ||
| if (len === 0) return data; | ||
| const rotated = new Uint8Array(len); | ||
| for (let i = 0; i < len; i++) { | ||
| rotated[(i + 1) % len] = data[i]; | ||
| } | ||
| return rotated; | ||
| } | ||
|  | ||
| function xorWithConst(data, constant) { | ||
| const result = new Uint8Array(data.length); | ||
| for (let i = 0; i < data.length; i++) { | ||
| result[i] = data[i] ^ constant; | ||
| } | ||
| return result; | ||
| } | ||
|  | ||
| const encryptionStages = [xorEncrypt, rotateBytes, xorWithConst]; | ||
|  | ||
| function multiStageEncrypt(data, password) { | ||
| let encrypted = data; | ||
| for (const stage of encryptionStages) { | ||
| encrypted = stage(encrypted, password); | ||
| } | ||
| return encrypted; | ||
| } | ||
|  | ||
| function multiStageDecrypt(data, password) { | ||
| let decrypted = data; | ||
| for (let i = encryptionStages.length - 1; i >= 0; i--) { | ||
| const stage = encryptionStages[i]; | ||
| if (stage === rotateBytes) { | ||
| const len = decrypted.length; | ||
| if (len > 0) { | ||
| const rotated = new Uint8Array(len); | ||
| for (let i = 0; i < len; i++) { | ||
| rotated[i] = decrypted[(i + 1) % len]; | ||
| } | ||
| decrypted = rotated; | ||
| } | ||
| } else { | ||
| decrypted = stage(decrypted, password); | ||
| } | ||
| } | ||
| return decrypted; | ||
| } | ||
|  | ||
| class Extension { | ||
| getInfo() { | ||
| return { | ||
| id: "SkyBuilder1717Encryption", | ||
| name: Scratch.translate("SK17"), | ||
| color1: "#3f79bf", | ||
| color2: "#2c4d8a", | ||
| blocks: [ | ||
| { | ||
| opcode: "encrypt", | ||
| blockType: Scratch.BlockType.REPORTER, | ||
| text: Scratch.translate("encrypt text [TEXT] with password [PASS]"), | ||
| arguments: { | ||
| TEXT: { | ||
| type: Scratch.ArgumentType.STRING, | ||
| defaultValue: "Hello, World!", | ||
| }, | ||
| PASS: { | ||
| type: Scratch.ArgumentType.STRING, | ||
| defaultValue: "password", | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| opcode: "decrypt", | ||
| blockType: Scratch.BlockType.REPORTER, | ||
| text: Scratch.translate("decrypt text [TEXT] with password [PASS]"), | ||
| arguments: { | ||
| TEXT: { type: Scratch.ArgumentType.STRING, defaultValue: "" }, | ||
| PASS: { | ||
| type: Scratch.ArgumentType.STRING, | ||
| defaultValue: "password", | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| opcode: "base64Encode", | ||
| blockType: Scratch.BlockType.REPORTER, | ||
| text: Scratch.translate("encode base64 bytes [TEXT]"), | ||
| arguments: { | ||
| TEXT: { | ||
| type: Scratch.ArgumentType.STRING, | ||
| defaultValue: "Hello", | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| opcode: "base64Decode", | ||
| blockType: Scratch.BlockType.REPORTER, | ||
| text: Scratch.translate("decode base64 bytes [TEXT]"), | ||
| arguments: { | ||
| TEXT: { | ||
| type: Scratch.ArgumentType.STRING, | ||
| defaultValue: "SGVsbG8=", | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| opcode: "checkSignature", | ||
| blockType: Scratch.BlockType.BOOLEAN, | ||
| text: Scratch.translate("verify signature [TEXT]"), | ||
| arguments: { | ||
| TEXT: { type: Scratch.ArgumentType.STRING, defaultValue: "" }, | ||
| }, | ||
| }, | ||
| { | ||
| opcode: "isValidEncrypted", | ||
| blockType: Scratch.BlockType.BOOLEAN, | ||
| text: Scratch.translate("is encrypted data valid [TEXT]"), | ||
| arguments: { | ||
| TEXT: { type: Scratch.ArgumentType.STRING, defaultValue: "" }, | ||
| }, | ||
| }, | ||
| ], | ||
| }; | ||
| } | ||
|  | ||
| async encrypt(args) { | ||
| const password = args.PASS; | ||
| const data = new TextEncoder().encode(args.TEXT); | ||
| const salt = crypto.getRandomValues(new Uint8Array(16)); | ||
| const iv = crypto.getRandomValues(new Uint8Array(12)); | ||
| const key = await deriveKey(password, salt); | ||
| const cipherBuf = await crypto.subtle.encrypt( | ||
| { name: "AES-GCM", iv: iv }, | ||
| key, | ||
| data | ||
| ); | ||
| let ciphertext = new Uint8Array(cipherBuf); | ||
| ciphertext = multiStageEncrypt(ciphertext, password); | ||
| const out = concat(SIG, salt, iv, ciphertext); | ||
| return await bytesToLatin1String(out); | ||
| } | ||
|  | ||
| async decrypt(args) { | ||
| const password = args.PASS; | ||
| const u8 = latin1StringToBytes(args.TEXT); | ||
| if (u8.length < SIG.length + 16 + 12 + 1) throw new Error("Invalid data"); | ||
| for (let i = 0; i < SIG.length; i++) { | ||
| if (u8[i] !== SIG[i]) throw new Error("Bad signature"); | ||
| } | ||
| let off = SIG.length; | ||
| const salt = u8.slice(off, off + 16); | ||
| off += 16; | ||
| const iv = u8.slice(off, off + 12); | ||
| off += 12; | ||
| let ciphertext = u8.slice(off); | ||
| ciphertext = multiStageDecrypt(ciphertext, password); | ||
| const key = await deriveKey(password, salt); | ||
| const plainBuf = await crypto.subtle.decrypt( | ||
| { name: "AES-GCM", iv: iv }, | ||
| key, | ||
| ciphertext | ||
| ); | ||
| return await new TextDecoder().decode(new Uint8Array(plainBuf)); | ||
| } | ||
|  | ||
| base64Encode(args) { | ||
| const u8 = new TextEncoder().encode(args.TEXT); | ||
| let binary = ""; | ||
| u8.forEach((b) => (binary += String.fromCharCode(b))); | ||
| return btoa(binary); | ||
| } | ||
|  | ||
| base64Decode(args) { | ||
| try { | ||
| const binary = atob(args.TEXT); | ||
| const u8 = new Uint8Array(binary.length); | ||
| for (let i = 0; i < binary.length; i++) { | ||
| u8[i] = binary.charCodeAt(i); | ||
| } | ||
| return new TextDecoder().decode(u8); | ||
| } catch { | ||
| return ""; | ||
| } | ||
| } | ||
|  | ||
| checkSignature(args) { | ||
| const text = args.TEXT; | ||
| if (typeof text !== "string") return false; | ||
| if (text.length < SIG.length) return false; | ||
| for (let i = 0; i < SIG.length; i++) { | ||
| if (text.charCodeAt(i) !== SIG[i]) return false; | ||
| } | ||
| return true; | ||
| } | ||
|  | ||
| isValidEncrypted(args) { | ||
| const text = args.TEXT; | ||
| if (typeof text !== "string") return false; | ||
| const u8 = latin1StringToBytes(text); | ||
| if (u8.length < SIG.length + 16 + 12 + 1) return false; | ||
| for (let i = 0; i < SIG.length; i++) { | ||
| if (u8[i] !== SIG[i]) return false; | ||
| } | ||
| return true; | ||
| } | ||
| } | ||
|  | ||
| Scratch.extensions.register(new Extension()); | ||
| })(Scratch); | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.