MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rename Ant.Crypto to crypto.()

+50 -54
+1 -1
README.txt
··· 52 52 - result.stdout - Raw stdout 53 53 - result.stderr - Raw stderr 54 54 55 - Cryptography (Ant.crypto): 55 + Cryptography (crypto): 56 56 - random() - Cryptographically secure random number 57 57 - randomBytes() - Generate random bytes 58 58 - randomUUID() - Generate UUID v4
+4 -4
examples/advanced.js
··· 1 - const uuid = '10000000-1000-4000-8000-100000000000'.replace(/[018]/g, c => 2 - (+c ^ (Ant.Crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (+c / 4)))).toString(16) 3 - ); 1 + import { uuid } from './lib/uuid'; 2 + import { nanoid } from './lib/nanoid'; 4 3 5 - console.log(uuid); 4 + console.log(uuid()); 5 + console.log(nanoid());
+1
examples/lib/nanoid.js
··· 1 + let a="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";export let nanoid=(e=21)=>{let t="",r=crypto.getRandomValues(new Uint8Array(e));for(let n=0;n<e;n++)t+=a[63&r[n]];return t};
+1
examples/lib/uuid.js
··· 1 + export let uuid=()=>"10000000-1000-4000-8000-100000000000".replace(/[018]/g,e=>(+e^crypto.getRandomValues(new Uint8Array(1))[0]&15>>+e/4).toString(16));
+1 -1
examples/uuid.min.js
··· 1 - function generateUUID(){const o=Ant.Crypto.randomBytes(16);o[6]=o[6]&15|64,o[8]=o[8]&63|128;const n=function(c){const t=o[c],e="0123456789abcdef",s=t>>4&15,r=t&15;return e[s]+e[r]};return n(0)+n(1)+n(2)+n(3)+"-"+n(4)+n(5)+"-"+n(6)+n(7)+"-"+n(8)+n(9)+"-"+n(10)+n(11)+n(12)+n(13)+n(14)+n(15)}console.log("builtin:",Ant.Crypto.randomUUID()),console.log("engine:",generateUUID()); 1 + function generateUUID(){const o=crypto.randomBytes(16);o[6]=o[6]&15|64,o[8]=o[8]&63|128;const n=function(c){const t=o[c],e="0123456789abcdef",s=t>>4&15,r=t&15;return e[s]+e[r]};return n(0)+n(1)+n(2)+n(3)+"-"+n(4)+n(5)+"-"+n(6)+n(7)+"-"+n(8)+n(9)+"-"+n(10)+n(11)+n(12)+n(13)+n(14)+n(15)}console.log("builtin:",crypto.randomUUID()),console.log("engine:",generateUUID());
+1 -1
meson.build
··· 74 74 build_date = run_command('date', '+%Y-%m-%d', check: true).stdout().strip() 75 75 76 76 version_conf = configuration_data() 77 - version_conf.set('ANT_VERSION', '0.0.8.21') 77 + version_conf.set('ANT_VERSION', '0.0.8.22') 78 78 version_conf.set('ANT_GIT_HASH', git_hash) 79 79 version_conf.set('ANT_BUILD_DATE', build_date) 80 80
+6 -9
src/modules/crypto.c
··· 40 40 return status; 41 41 } 42 42 43 - // Ant.crypto.random() 43 + // crypto.random() 44 44 static jsval_t js_crypto_random(struct js *js, jsval_t *args, int nargs) { 45 45 (void) args; 46 46 (void) nargs; ··· 54 54 return js_mknum((double)value); 55 55 } 56 56 57 - // Ant.Crypto.randomBytes(length) 57 + // crypto.randomBytes(length) 58 58 static jsval_t js_crypto_random_bytes(struct js *js, jsval_t *args, int nargs) { 59 59 if (nargs < 1) { 60 60 return js_mkerr(js, "randomBytes requires a length argument"); ··· 90 90 return array; 91 91 } 92 92 93 - // Ant.Crypto.randomUUID() 93 + // crypto.randomUUID() 94 94 static jsval_t js_crypto_random_uuid(struct js *js, jsval_t *args, int nargs) { 95 95 (void) args; 96 96 (void) nargs; ··· 108 108 return js_mkstr(js, uuid_str, strlen(uuid_str)); 109 109 } 110 110 111 - // Ant.Crypto.randomUUIDv7() 111 + // crypto.randomUUIDv7() 112 112 static jsval_t js_crypto_random_uuidv7(struct js *js, jsval_t *args, int nargs) { 113 113 (void) args; 114 114 (void) nargs; ··· 130 130 return js_mkstr(js, uuid_str, strlen(uuid_str)); 131 131 } 132 132 133 - // Ant.Crypto.getRandomValues(typedArray) 133 + // crypto.getRandomValues(typedArray) 134 134 static jsval_t js_crypto_get_random_values(struct js *js, jsval_t *args, int nargs) { 135 135 if (nargs < 1) { 136 136 return js_mkerr(js, "getRandomValues requires a TypedArray argument"); ··· 164 164 165 165 void init_crypto_module() { 166 166 struct js *js = rt->js; 167 - jsval_t ant_obj = rt->ant_obj; 168 - 169 167 jsval_t crypto_obj = js_mkobj(js); 170 168 171 169 js_set(js, crypto_obj, "random", js_mkfun(js_crypto_random)); ··· 175 173 js_set(js, crypto_obj, "getRandomValues", js_mkfun(js_crypto_get_random_values)); 176 174 177 175 js_set(js, crypto_obj, "@@toStringTag", js_mkstr(js, "Crypto", 6)); 178 - js_set(js, ant_obj, "Crypto", crypto_obj); 176 + js_set(js, js_glob(js), "crypto", crypto_obj); 179 177 } 180 -
+1 -1
tests/nanoid.cjs
··· 2 2 3 3 function nanoid(size) { 4 4 let id = ''; 5 - const randomBytes = Ant.Crypto.randomBytes(size); 5 + const randomBytes = crypto.randomBytes(size); 6 6 7 7 for (let i = 0; i < size; i++) { 8 8 id += alphabet[63 & randomBytes[i]];
+14 -14
tests/test_all_modules.cjs
··· 1 1 // Test toStringTag for all modules 2 2 3 - console.log("Testing @@toStringTag for all modules:\n"); 3 + console.log('Testing @@toStringTag for all modules:\n'); 4 4 5 5 // Built-in modules 6 - console.log("Atomics:", Object.prototype.toString.call(Atomics)); 7 - console.log("console:", Object.prototype.toString.call(console)); 8 - console.log("JSON:", Object.prototype.toString.call(JSON)); 9 - console.log("process:", Object.prototype.toString.call(process)); 10 - console.log("Buffer:", Object.prototype.toString.call(Buffer)); 6 + console.log('Atomics:', Object.prototype.toString.call(Atomics)); 7 + console.log('console:', Object.prototype.toString.call(console)); 8 + console.log('JSON:', Object.prototype.toString.call(JSON)); 9 + console.log('process:', Object.prototype.toString.call(process)); 10 + console.log('Buffer:', Object.prototype.toString.call(Buffer)); 11 11 12 - // Test Ant.Crypto if available 13 - if (typeof Ant !== 'undefined' && Ant.Crypto) { 14 - console.log("Ant.Crypto:", Object.prototype.toString.call(Ant.Crypto)); 12 + // Test crypto if available 13 + if (typeof Ant !== 'undefined' && crypto) { 14 + console.log('crypto:', Object.prototype.toString.call(crypto)); 15 15 } 16 16 17 17 // Test imported modules 18 - console.log("\nTesting imported modules:"); 18 + console.log('\nTesting imported modules:'); 19 19 20 20 // Import path module 21 21 import * as path from 'ant:path'; 22 - console.log("path:", Object.prototype.toString.call(path)); 22 + console.log('path:', Object.prototype.toString.call(path)); 23 23 24 24 // Import fs module 25 25 import * as fs from 'ant:fs'; 26 - console.log("fs:", Object.prototype.toString.call(fs)); 26 + console.log('fs:', Object.prototype.toString.call(fs)); 27 27 28 28 // Import shell module 29 29 import * as shell from 'ant:shell'; 30 - console.log("shell:", Object.prototype.toString.call(shell)); 30 + console.log('shell:', Object.prototype.toString.call(shell)); 31 31 32 32 // Import ffi module 33 33 import * as ffi from 'ant:ffi'; 34 - console.log("ffi:", Object.prototype.toString.call(ffi)); 34 + console.log('ffi:', Object.prototype.toString.call(ffi));
+2 -2
tests/uuid.cjs
··· 1 1 function generateUUID() { 2 - const bytes = Ant.Crypto.randomBytes(16); 2 + const bytes = crypto.randomBytes(16); 3 3 4 4 bytes[6] = (bytes[6] & 0x0f) | 0x40; 5 5 bytes[8] = (bytes[8] & 0x3f) | 0x80; ··· 36 36 ); 37 37 } 38 38 39 - console.log('builtin:', Ant.Crypto.randomUUID()); 39 + console.log('builtin:', crypto.randomUUID()); 40 40 console.log('engine:', generateUUID());
+18 -21
tests/uuidv7.cjs
··· 1 - // Test UUIDv7 generation from Ant.Crypto.randomUUIDv7() 1 + // Test UUIDv7 generation from crypto.randomUUIDv7() 2 2 3 3 function validateUUIDv7Format(uuid) { 4 4 // Check format: xxxxxxxx-xxxx-7xxx-xxxx-xxxxxxxxxxxx 5 5 // Must be exactly 36 characters 6 6 if (uuid.length !== 36) return false; 7 - 7 + 8 8 // Check dash positions 9 9 if (uuid[8] !== '-' || uuid[13] !== '-' || uuid[18] !== '-' || uuid[23] !== '-') { 10 10 return false; 11 11 } 12 - 12 + 13 13 // Check version digit (should be 7) 14 14 if (uuid[14] !== '7') return false; 15 - 15 + 16 16 // Check variant digit (should be 8, 9, a, or b) 17 17 const variantChar = uuid[19]; 18 18 if (variantChar !== '8' && variantChar !== '9' && variantChar !== 'a' && variantChar !== 'b') { 19 19 return false; 20 20 } 21 - 21 + 22 22 // Check all other characters are hex digits 23 23 for (let i = 0; i < uuid.length; i++) { 24 24 if (i === 8 || i === 13 || i === 18 || i === 23) continue; // Skip dashes ··· 26 26 const isHex = (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'); 27 27 if (!isHex) return false; 28 28 } 29 - 29 + 30 30 return true; 31 31 } 32 32 ··· 38 38 hex = hex.replace('-', ''); 39 39 hex = hex.replace('-', ''); 40 40 hex = hex.replace('-', ''); 41 - 41 + 42 42 const timestampHex = hex.substring(0, 12); 43 - 43 + 44 44 // Convert hex to number (timestamp in milliseconds) 45 45 let timestamp = 0; 46 46 for (let i = 0; i < timestampHex.length; i++) { 47 47 const digit = timestampHex[i]; 48 - const value = digit >= '0' && digit <= '9' 49 - ? digit.charCodeAt(0) - '0'.charCodeAt(0) 50 - : digit.charCodeAt(0) - 'a'.charCodeAt(0) + 10; 48 + const value = digit >= '0' && digit <= '9' ? digit.charCodeAt(0) - '0'.charCodeAt(0) : digit.charCodeAt(0) - 'a'.charCodeAt(0) + 10; 51 49 timestamp = timestamp * 16 + value; 52 50 } 53 - 51 + 54 52 return timestamp; 55 53 } 56 54 57 55 // Test 1: Generate and validate format 58 56 console.log('Test 1: Basic UUIDv7 generation'); 59 - const uuid1 = Ant.Crypto.randomUUIDv7(); 57 + const uuid1 = crypto.randomUUIDv7(); 60 58 console.log('Generated:', uuid1); 61 59 console.log('Valid format:', validateUUIDv7Format(uuid1) ? 'PASS' : 'FAIL'); 62 60 console.log('Length:', uuid1.length === 36 ? 'PASS' : 'FAIL'); ··· 67 65 const uuids = []; 68 66 const count = 100; 69 67 for (let i = 0; i < count; i++) { 70 - uuids[i] = Ant.Crypto.randomUUIDv7(); 68 + uuids[i] = crypto.randomUUIDv7(); 71 69 } 72 70 73 71 let allUnique = true; ··· 86 84 87 85 // Test 3: Timestamp ordering 88 86 console.log('Test 3: Timestamp ordering'); 89 - const uuid2 = Ant.Crypto.randomUUIDv7(); 90 - const uuid3 = Ant.Crypto.randomUUIDv7(); 87 + const uuid2 = crypto.randomUUIDv7(); 88 + const uuid3 = crypto.randomUUIDv7(); 91 89 92 90 const ts2 = extractTimestamp(uuid2); 93 91 const ts3 = extractTimestamp(uuid3); ··· 101 99 102 100 // Test 4: Version and variant bits 103 101 console.log('Test 4: Version and variant validation'); 104 - const testUuid = Ant.Crypto.randomUUIDv7(); 102 + const testUuid = crypto.randomUUIDv7(); 105 103 const parts = testUuid.split('-'); 106 104 107 105 // Check version (4 bits at position 48-51, should be 0111 = 7) ··· 111 109 112 110 // Check variant (2 bits at position 64-65, should be 10) 113 111 const variantChar = parts[3][0]; 114 - const variantValid = variantChar === '8' || variantChar === '9' || 115 - variantChar === 'a' || variantChar === 'b'; 112 + const variantValid = variantChar === '8' || variantChar === '9' || variantChar === 'a' || variantChar === 'b'; 116 113 console.log('Variant character:', variantChar); 117 114 console.log('Variant is RFC 4122:', variantValid ? 'PASS' : 'FAIL'); 118 115 console.log(''); 119 116 120 117 // Test 5: Timestamp reasonableness 121 118 console.log('Test 5: Timestamp reasonableness'); 122 - const currentUuid = Ant.Crypto.randomUUIDv7(); 119 + const currentUuid = crypto.randomUUIDv7(); 123 120 const currentTs = extractTimestamp(currentUuid); 124 121 125 122 // Timestamp should be reasonable (after 2020-01-01 and before 2100-01-01) ··· 134 131 // Test 6: Display multiple UUIDs 135 132 console.log('Test 6: Sample UUIDv7s'); 136 133 for (let i = 0; i < 5; i++) { 137 - console.log(i + 1 + ':', Ant.Crypto.randomUUIDv7()); 134 + console.log(i + 1 + ':', crypto.randomUUIDv7()); 138 135 }