From 350e0323d581791c648bebb7e2724e6ac11993d9 Mon Sep 17 00:00:00 2001 From: theMackabu Date: Tue, 16 Dec 2025 16:48:32 -0800 Subject: [PATCH] rename Ant.Crypto to crypto.() --- README.txt | 2 +- examples/advanced.js | 8 ++++---- examples/lib/nanoid.js | 1 + examples/lib/uuid.js | 1 + examples/uuid.min.js | 2 +- meson.build | 2 +- src/modules/crypto.c | 15 ++++++--------- tests/nanoid.cjs | 2 +- tests/test_all_modules.cjs | 30 ++++++++++++++--------------- tests/uuid.cjs | 4 ++-- tests/uuidv7.cjs | 39 ++++++++++++++++++-------------------- 11 files changed, 51 insertions(+), 55 deletions(-) create mode 100644 examples/lib/nanoid.js create mode 100644 examples/lib/uuid.js diff --git a/README.txt b/README.txt index 671f39e..41fbcb1 100644 --- a/README.txt +++ b/README.txt @@ -52,7 +52,7 @@ MODULES: - result.stdout - Raw stdout - result.stderr - Raw stderr - Cryptography (Ant.crypto): + Cryptography (crypto): - random() - Cryptographically secure random number - randomBytes() - Generate random bytes - randomUUID() - Generate UUID v4 diff --git a/examples/advanced.js b/examples/advanced.js index 570ddb7..3933f8b 100644 --- a/examples/advanced.js +++ b/examples/advanced.js @@ -1,5 +1,5 @@ -const uuid = '10000000-1000-4000-8000-100000000000'.replace(/[018]/g, c => - (+c ^ (Ant.Crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (+c / 4)))).toString(16) -); +import { uuid } from './lib/uuid'; +import { nanoid } from './lib/nanoid'; -console.log(uuid); +console.log(uuid()); +console.log(nanoid()); diff --git a/examples/lib/nanoid.js b/examples/lib/nanoid.js new file mode 100644 index 0000000..ffa1d4b --- /dev/null +++ b/examples/lib/nanoid.js @@ -0,0 +1 @@ +let a="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";export let nanoid=(e=21)=>{let t="",r=crypto.getRandomValues(new Uint8Array(e));for(let n=0;n"10000000-1000-4000-8000-100000000000".replace(/[018]/g,e=>(+e^crypto.getRandomValues(new Uint8Array(1))[0]&15>>+e/4).toString(16)); \ No newline at end of file diff --git a/examples/uuid.min.js b/examples/uuid.min.js index e6ab2c0..109e9b7 100644 --- a/examples/uuid.min.js +++ b/examples/uuid.min.js @@ -1 +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()); \ No newline at end of file +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()); \ No newline at end of file diff --git a/meson.build b/meson.build index 2c8e05c..bb4f6ef 100644 --- a/meson.build +++ b/meson.build @@ -74,7 +74,7 @@ endif build_date = run_command('date', '+%Y-%m-%d', check: true).stdout().strip() version_conf = configuration_data() -version_conf.set('ANT_VERSION', '0.0.8.21') +version_conf.set('ANT_VERSION', '0.0.8.22') version_conf.set('ANT_GIT_HASH', git_hash) version_conf.set('ANT_BUILD_DATE', build_date) diff --git a/src/modules/crypto.c b/src/modules/crypto.c index cc8627e..787ee19 100644 --- a/src/modules/crypto.c +++ b/src/modules/crypto.c @@ -40,7 +40,7 @@ int uuidv7_new(uint8_t *uuid_out) { return status; } -// Ant.crypto.random() +// crypto.random() static jsval_t js_crypto_random(struct js *js, jsval_t *args, int nargs) { (void) args; (void) nargs; @@ -54,7 +54,7 @@ static jsval_t js_crypto_random(struct js *js, jsval_t *args, int nargs) { return js_mknum((double)value); } -// Ant.Crypto.randomBytes(length) +// crypto.randomBytes(length) static jsval_t js_crypto_random_bytes(struct js *js, jsval_t *args, int nargs) { if (nargs < 1) { return js_mkerr(js, "randomBytes requires a length argument"); @@ -90,7 +90,7 @@ static jsval_t js_crypto_random_bytes(struct js *js, jsval_t *args, int nargs) { return array; } -// Ant.Crypto.randomUUID() +// crypto.randomUUID() static jsval_t js_crypto_random_uuid(struct js *js, jsval_t *args, int nargs) { (void) args; (void) nargs; @@ -108,7 +108,7 @@ static jsval_t js_crypto_random_uuid(struct js *js, jsval_t *args, int nargs) { return js_mkstr(js, uuid_str, strlen(uuid_str)); } -// Ant.Crypto.randomUUIDv7() +// crypto.randomUUIDv7() static jsval_t js_crypto_random_uuidv7(struct js *js, jsval_t *args, int nargs) { (void) args; (void) nargs; @@ -130,7 +130,7 @@ static jsval_t js_crypto_random_uuidv7(struct js *js, jsval_t *args, int nargs) return js_mkstr(js, uuid_str, strlen(uuid_str)); } -// Ant.Crypto.getRandomValues(typedArray) +// crypto.getRandomValues(typedArray) static jsval_t js_crypto_get_random_values(struct js *js, jsval_t *args, int nargs) { if (nargs < 1) { return js_mkerr(js, "getRandomValues requires a TypedArray argument"); @@ -164,8 +164,6 @@ static jsval_t js_crypto_get_random_values(struct js *js, jsval_t *args, int nar void init_crypto_module() { struct js *js = rt->js; - jsval_t ant_obj = rt->ant_obj; - jsval_t crypto_obj = js_mkobj(js); js_set(js, crypto_obj, "random", js_mkfun(js_crypto_random)); @@ -175,6 +173,5 @@ void init_crypto_module() { js_set(js, crypto_obj, "getRandomValues", js_mkfun(js_crypto_get_random_values)); js_set(js, crypto_obj, "@@toStringTag", js_mkstr(js, "Crypto", 6)); - js_set(js, ant_obj, "Crypto", crypto_obj); + js_set(js, js_glob(js), "crypto", crypto_obj); } - diff --git a/tests/nanoid.cjs b/tests/nanoid.cjs index 89a3fcf..e6d387b 100644 --- a/tests/nanoid.cjs +++ b/tests/nanoid.cjs @@ -2,7 +2,7 @@ let alphabet = 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict function nanoid(size) { let id = ''; - const randomBytes = Ant.Crypto.randomBytes(size); + const randomBytes = crypto.randomBytes(size); for (let i = 0; i < size; i++) { id += alphabet[63 & randomBytes[i]]; diff --git a/tests/test_all_modules.cjs b/tests/test_all_modules.cjs index 42676fc..ffeed91 100644 --- a/tests/test_all_modules.cjs +++ b/tests/test_all_modules.cjs @@ -1,34 +1,34 @@ // Test toStringTag for all modules -console.log("Testing @@toStringTag for all modules:\n"); +console.log('Testing @@toStringTag for all modules:\n'); // Built-in modules -console.log("Atomics:", Object.prototype.toString.call(Atomics)); -console.log("console:", Object.prototype.toString.call(console)); -console.log("JSON:", Object.prototype.toString.call(JSON)); -console.log("process:", Object.prototype.toString.call(process)); -console.log("Buffer:", Object.prototype.toString.call(Buffer)); - -// Test Ant.Crypto if available -if (typeof Ant !== 'undefined' && Ant.Crypto) { - console.log("Ant.Crypto:", Object.prototype.toString.call(Ant.Crypto)); +console.log('Atomics:', Object.prototype.toString.call(Atomics)); +console.log('console:', Object.prototype.toString.call(console)); +console.log('JSON:', Object.prototype.toString.call(JSON)); +console.log('process:', Object.prototype.toString.call(process)); +console.log('Buffer:', Object.prototype.toString.call(Buffer)); + +// Test crypto if available +if (typeof Ant !== 'undefined' && crypto) { + console.log('crypto:', Object.prototype.toString.call(crypto)); } // Test imported modules -console.log("\nTesting imported modules:"); +console.log('\nTesting imported modules:'); // Import path module import * as path from 'ant:path'; -console.log("path:", Object.prototype.toString.call(path)); +console.log('path:', Object.prototype.toString.call(path)); // Import fs module import * as fs from 'ant:fs'; -console.log("fs:", Object.prototype.toString.call(fs)); +console.log('fs:', Object.prototype.toString.call(fs)); // Import shell module import * as shell from 'ant:shell'; -console.log("shell:", Object.prototype.toString.call(shell)); +console.log('shell:', Object.prototype.toString.call(shell)); // Import ffi module import * as ffi from 'ant:ffi'; -console.log("ffi:", Object.prototype.toString.call(ffi)); +console.log('ffi:', Object.prototype.toString.call(ffi)); diff --git a/tests/uuid.cjs b/tests/uuid.cjs index 99536a1..988bce5 100644 --- a/tests/uuid.cjs +++ b/tests/uuid.cjs @@ -1,5 +1,5 @@ function generateUUID() { - const bytes = Ant.Crypto.randomBytes(16); + const bytes = crypto.randomBytes(16); bytes[6] = (bytes[6] & 0x0f) | 0x40; bytes[8] = (bytes[8] & 0x3f) | 0x80; @@ -36,5 +36,5 @@ function generateUUID() { ); } -console.log('builtin:', Ant.Crypto.randomUUID()); +console.log('builtin:', crypto.randomUUID()); console.log('engine:', generateUUID()); diff --git a/tests/uuidv7.cjs b/tests/uuidv7.cjs index 4702abc..4af9841 100644 --- a/tests/uuidv7.cjs +++ b/tests/uuidv7.cjs @@ -1,24 +1,24 @@ -// Test UUIDv7 generation from Ant.Crypto.randomUUIDv7() +// Test UUIDv7 generation from crypto.randomUUIDv7() function validateUUIDv7Format(uuid) { // Check format: xxxxxxxx-xxxx-7xxx-xxxx-xxxxxxxxxxxx // Must be exactly 36 characters if (uuid.length !== 36) return false; - + // Check dash positions if (uuid[8] !== '-' || uuid[13] !== '-' || uuid[18] !== '-' || uuid[23] !== '-') { return false; } - + // Check version digit (should be 7) if (uuid[14] !== '7') return false; - + // Check variant digit (should be 8, 9, a, or b) const variantChar = uuid[19]; if (variantChar !== '8' && variantChar !== '9' && variantChar !== 'a' && variantChar !== 'b') { return false; } - + // Check all other characters are hex digits for (let i = 0; i < uuid.length; i++) { if (i === 8 || i === 13 || i === 18 || i === 23) continue; // Skip dashes @@ -26,7 +26,7 @@ function validateUUIDv7Format(uuid) { const isHex = (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'); if (!isHex) return false; } - + return true; } @@ -38,25 +38,23 @@ function extractTimestamp(uuid) { hex = hex.replace('-', ''); hex = hex.replace('-', ''); hex = hex.replace('-', ''); - + const timestampHex = hex.substring(0, 12); - + // Convert hex to number (timestamp in milliseconds) let timestamp = 0; for (let i = 0; i < timestampHex.length; i++) { const digit = timestampHex[i]; - const value = digit >= '0' && digit <= '9' - ? digit.charCodeAt(0) - '0'.charCodeAt(0) - : digit.charCodeAt(0) - 'a'.charCodeAt(0) + 10; + const value = digit >= '0' && digit <= '9' ? digit.charCodeAt(0) - '0'.charCodeAt(0) : digit.charCodeAt(0) - 'a'.charCodeAt(0) + 10; timestamp = timestamp * 16 + value; } - + return timestamp; } // Test 1: Generate and validate format console.log('Test 1: Basic UUIDv7 generation'); -const uuid1 = Ant.Crypto.randomUUIDv7(); +const uuid1 = crypto.randomUUIDv7(); console.log('Generated:', uuid1); console.log('Valid format:', validateUUIDv7Format(uuid1) ? 'PASS' : 'FAIL'); console.log('Length:', uuid1.length === 36 ? 'PASS' : 'FAIL'); @@ -67,7 +65,7 @@ console.log('Test 2: Uniqueness check'); const uuids = []; const count = 100; for (let i = 0; i < count; i++) { - uuids[i] = Ant.Crypto.randomUUIDv7(); + uuids[i] = crypto.randomUUIDv7(); } let allUnique = true; @@ -86,8 +84,8 @@ console.log(''); // Test 3: Timestamp ordering console.log('Test 3: Timestamp ordering'); -const uuid2 = Ant.Crypto.randomUUIDv7(); -const uuid3 = Ant.Crypto.randomUUIDv7(); +const uuid2 = crypto.randomUUIDv7(); +const uuid3 = crypto.randomUUIDv7(); const ts2 = extractTimestamp(uuid2); const ts3 = extractTimestamp(uuid3); @@ -101,7 +99,7 @@ console.log(''); // Test 4: Version and variant bits console.log('Test 4: Version and variant validation'); -const testUuid = Ant.Crypto.randomUUIDv7(); +const testUuid = crypto.randomUUIDv7(); const parts = testUuid.split('-'); // Check version (4 bits at position 48-51, should be 0111 = 7) @@ -111,15 +109,14 @@ console.log('Version is 7:', versionChar === '7' ? 'PASS' : 'FAIL'); // Check variant (2 bits at position 64-65, should be 10) const variantChar = parts[3][0]; -const variantValid = variantChar === '8' || variantChar === '9' || - variantChar === 'a' || variantChar === 'b'; +const variantValid = variantChar === '8' || variantChar === '9' || variantChar === 'a' || variantChar === 'b'; console.log('Variant character:', variantChar); console.log('Variant is RFC 4122:', variantValid ? 'PASS' : 'FAIL'); console.log(''); // Test 5: Timestamp reasonableness console.log('Test 5: Timestamp reasonableness'); -const currentUuid = Ant.Crypto.randomUUIDv7(); +const currentUuid = crypto.randomUUIDv7(); const currentTs = extractTimestamp(currentUuid); // Timestamp should be reasonable (after 2020-01-01 and before 2100-01-01) @@ -134,5 +131,5 @@ console.log(''); // Test 6: Display multiple UUIDs console.log('Test 6: Sample UUIDv7s'); for (let i = 0; i < 5; i++) { - console.log(i + 1 + ':', Ant.Crypto.randomUUIDv7()); + console.log(i + 1 + ':', crypto.randomUUIDv7()); } -- 2.51.2