/* DevBox · 编码 / 字符 / 密码学补齐 */ (function () { 'use strict' const { textTool: T, convTool: C, genTool: G } = DB const enc = (s) => new TextEncoder().encode(s) const hex = (b) => Array.from(b, (x) => x.toString(16).padStart(2, '0')).join('') const fromHex = (s) => new Uint8Array((s.trim().match(/[\da-fA-F]{2}/g) || []).map((h) => parseInt(h, 16))) const dec = (b) => new TextDecoder().decode(b) C({ id: 'url-encode', name: 'URL 编解码', group: 'encode', icon: 'globe', desc: '整串编码、仅参数值编码、解码都给你', keywords: ['url', 'encode', '编码', '百分号'], left: '原文', right: '编码后', a2b: (s) => encodeURIComponent(s), b2a: (s) => decodeURIComponent(s), phA: 'a=1&b=中文' }) T({ id: 'url-parser', name: 'URL 解析', group: 'web', icon: 'globe', desc: '拆出协议、主机、端口、路径、查询参数,键值一行一个', keywords: ['url', 'parse', '解析', '参数'], ph: 'https://a.com:8443/x/y?k=1&k=2&z=中文#h', fn: (s) => { const u = new URL(s.trim()); const rows = [`协议 ${u.protocol}`, `主机 ${u.hostname}`, `端口 ${u.port || '(默认)'}`, `路径 ${u.pathname}`, `查询 ${u.search}`, `锚点 ${u.hash}`, '', '参数:'] for (const [k, v] of u.searchParams) rows.push(` ${k} = ${v}`) return rows.join('\n') } }) T({ id: 'html-entities', name: 'HTML 实体转义', group: 'encode', icon: 'code', desc: 'HTML 转义与反转义,防止 XSS、贴代码用', keywords: ['html', 'entities', '转义', 'xss'], ph: '
你好 & 再见
', fn: (s) => { const esc = s.replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"').replace(/'/g, ''') const ta = document.createElement('textarea'); ta.innerHTML = s return `转义:\n${esc}\n\n反转义:\n${ta.value}` } }) T({ id: 'html-strip', name: '去 HTML 标签', group: 'web', icon: 'code', desc: '把网页源码扒成纯文本,保留段落换行', keywords: ['html', 'strip', '去标签', '纯文本'], ph: '

段落

粗体', fn: (s) => s.replace(/<(script|style)[\s\S]*?<\/\1>/gi, '').replace(//gi, '\n').replace(/<\/(p|div|li|h[1-6])>/gi, '\n').replace(/<[^>]+>/g, '').replace(/ /g, ' ').replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/\n{3,}/g, '\n\n').trim() }) T({ id: 'html-to-markdown', name: 'HTML → Markdown', group: 'format', icon: 'swap', desc: '常见标签转 Markdown:标题、加粗、链接、列表、代码、图片', keywords: ['html', 'markdown', '转换'], ph: '

标题

正文 粗 链

', fn: (s) => s.replace(/]*>([\s\S]*?)<\/h\1>/gi, (_, n, t) => '\n' + '#'.repeat(+n) + ' ' + t + '\n') .replace(/]*>([\s\S]*?)<\/strong>/gi, '**$1**').replace(/]*>([\s\S]*?)<\/b>/gi, '**$1**') .replace(/]*>([\s\S]*?)<\/em>/gi, '*$1*').replace(/]*>([\s\S]*?)<\/code>/gi, '`$1`') .replace(/]*href="([^"]*)"[^>]*>([\s\S]*?)<\/a>/gi, '[$2]($1)').replace(/]*src="([^"]*)"[^>]*alt="([^"]*)"[^>]*>/gi, '![$2]($1)') .replace(/]*>([\s\S]*?)<\/li>/gi, '- $1\n').replace(/<\/p>/gi, '\n\n').replace(/<[^>]+>/g, '').replace(/\n{3,}/g, '\n\n').trim() }) T({ id: 'unicode-convert', name: 'Unicode 转换', group: 'encode', icon: 'code', desc: '中文转 \\u 转义、转 HTML 实体、转 UTF-8 字节', keywords: ['unicode', '转义', 'utf8'], ph: '哈基米', fn: (s) => `\\u 转义:${Array.from(s).map((c) => '\\u' + c.charCodeAt(0).toString(16).toUpperCase().padStart(4, '0')).join('')}\nHTML 实体:${Array.from(s).map((c) => '&#' + c.codePointAt(0) + ';').join('')}\nUTF-8 字节:${hex(enc(s))}\n码点:${Array.from(s).map((c) => 'U+' + c.codePointAt(0).toString(16).toUpperCase().padStart(4, '0')).join(' ')}` }) T({ id: 'ascii-table', name: 'ASCII 码表', group: 'encode', icon: 'code', desc: '0–127 全表:十进制、十六进制、字符、控制符说明', keywords: ['ascii', '码表'], ph: '(结果在下)', fn: () => { const ctl = { 0: 'NUL', 9: 'TAB', 10: 'LF', 13: 'CR', 27: 'ESC', 32: 'SPACE', 127: 'DEL' } return Array.from({ length: 128 }, (_, i) => { const c = String.fromCharCode(i); const show = i < 32 || i === 127 ? (ctl[i] || '^' + String.fromCharCode(i + 64)) : c return `${String(i).padStart(3, ' ')} 0x${i.toString(16).padStart(2, '0')} ${show}` }).join('\n') } }) C({ id: 'base-converter', name: '进制转换', group: 'encode', icon: 'hash', desc: '2 / 8 / 10 / 16 / 36 进制互转,带分组显示', keywords: ['base', '进制', '二进制', '十六进制'], left: '十进制', right: '其他进制', a2b: (s) => { const n = BigInt(s.trim()); return `2 进制 ${n.toString(2)}\n8 进制 ${n.toString(8)}\n16 进制 ${n.toString(16).toUpperCase()}\n36 进制 ${n.toString(36)}` }, b2a: (s) => { const t = s.trim(); const n = /^0x/i.test(t) ? BigInt(t) : BigInt('0x' + t); return n.toString(10) }, phA: '255' }) C({ id: 'morse-code', name: '摩斯密码', group: 'encode', icon: 'hash', desc: '文字转摩斯、摩斯转文字,支持数字与常用标点', keywords: ['morse', '摩斯', '电报'], left: '文字', right: '摩斯', a2b: (s) => { const M = { a: '.-', b: '-...', c: '-.-.', d: '-..', e: '.', f: '..-.', g: '--.', h: '....', i: '..', j: '.---', k: '-.-', l: '.-..', m: '--', n: '-.', o: '---', p: '.--.', q: '--.-', r: '.-.', s: '...', t: '-', u: '..-', v: '...-', w: '.--', x: '-..-', y: '-.--', z: '--..', 0: '-----', 1: '.----', 2: '..---', 3: '...--', 4: '....-', 5: '.....', 6: '-....', 7: '--...', 8: '---..', 9: '----.' } return s.toLowerCase().split(/\s+/).map((w) => Array.from(w).map((c) => M[c] || c).join(' ')).join(' / ') }, b2a: (s) => { const R = { '.-': 'a', '-...': 'b', '-.-.': 'c', '-..': 'd', '.': 'e', '..-.': 'f', '--.': 'g', '....': 'h', '..': 'i', '.---': 'j', '-.-': 'k', '.-..': 'l', '--': 'm', '-.': 'n', '---': 'o', '.--.': 'p', '--.-': 'q', '.-.': 'r', '...': 's', '-': 't', '..-': 'u', '...-': 'v', '.--': 'w', '-..-': 'x', '-.--': 'y', '--..': 'z', '-----': '0', '.----': '1', '..---': '2', '...--': '3', '....-': '4', '.....': '5', '-....': '6', '--...': '7', '---..': '8', '----.': '9' } return s.trim().split(/\s*\/\s*/).map((w) => w.trim().split(/\s+/).map((m) => R[m] || '').join('')).join(' ') }, phA: 'sos help' }) T({ id: 'braille-convert', name: '盲文转换', group: 'encode', icon: 'hash', desc: '英文盲文字符转换(Unicode 盲文块)', keywords: ['braille', '盲文'], ph: 'hello', fn: (s) => s.toLowerCase().replace(/[a-z]/g, (c) => String.fromCharCode(0x2800 + (1 << (c.charCodeAt(0) - 97)))).split('').join('') + '\n(盲文字符可能显示很小,属正常)' }) T({ id: 'nato-alphabet', name: '字母报读表', group: 'encode', icon: 'hash', desc: 'NATO 音标字母:打电话报编码不出错', keywords: ['nato', '报读', '字母表'], ph: 'abc', fn: (s) => { const N = { a: 'Alpha', b: 'Bravo', c: 'Charlie', d: 'Delta', e: 'Echo', f: 'Foxtrot', g: 'Golf', h: 'Hotel', i: 'India', j: 'Juliett', k: 'Kilo', l: 'Lima', m: 'Mike', n: 'November', o: 'Oscar', p: 'Papa', q: 'Quebec', r: 'Romeo', s: 'Sierra', t: 'Tango', u: 'Uniform', v: 'Victor', w: 'Whiskey', x: 'Xray', y: 'Yankee', z: 'Zulu' } return Array.from(s.toLowerCase().replace(/[^a-z]/g, '')).map((c) => N[c]).join(' ') } }) T({ id: 'chinese-number', name: '中文数字转换', group: 'text', icon: 'text', desc: '阿拉伯数字 ↔ 中文数字(含大写),读稿子方便', keywords: ['中文数字', '大写', '人民币'], ph: '1234567890', fn: (s) => { const D = '零一二三四五六七八九', U = ['', '十', '百', '千', '万', '亿'], big = '零壹贰叁肆伍陆柒捌玖' const conv = (n, digits) => { const x = String(n); if (x.length > 12) return '数字太大' let out = '', zero = false for (let i = 0; i < x.length; i++) { const d = +x[i], pos = x.length - 1 - i if (!d) { zero = out !== ''; continue } if (zero) { out += digits[0]; zero = false } out += digits[d] + (U[pos] || '') } return out.replace(/^一十/, '十') } return `小写:${conv(s.trim(), D)}\n大写:${conv(s.trim(), big)}` } }) C({ id: 'fullwidth-convert', name: '全角半角转换', group: 'encode', icon: 'text', desc: '全角 ↔ 半角,中英混排时对齐用', keywords: ['fullwidth', '全角', '半角'], left: '半角', right: '全角', a2b: (s) => Array.from(s, (c) => (c === ' ' ? ' ' : c.charCodeAt(0) >= 33 && c.charCodeAt(0) <= 126 ? String.fromCharCode(c.charCodeAt(0) + 0xfee0) : c)).join(''), b2a: (s) => Array.from(s, (c) => { const n = c.charCodeAt(0); return n === 0x3000 ? ' ' : n >= 0xff01 && n <= 0xff5e ? String.fromCharCode(n - 0xfee0) : c }).join(''), phA: 'ABC 123' }) C({ id: 'punctuation-convert', name: '标点转换', group: 'text', icon: 'text', desc: '中英文标点互转,引用文章前后统一', keywords: ['标点', 'punctuation', '中文标点'], left: '英文标点', right: '中文标点', a2b: (s) => s.replace(/,/g, ',').replace(/\./g, '。').replace(/\?/g, '?').replace(/!/g, '!').replace(/:/g, ':').replace(/;/g, ';').replace(/\("([^"]*)"\)/g, '(“$1”)').replace(/"([^"]*)"/g, '“$1”').replace(/'([^']*)'/g, '‘$1’'), b2a: (s) => s.replace(/,/g, ',').replace(/。/g, '.').replace(/?/g, '?').replace(/!/g, '!').replace(/:/g, ':').replace(/;/g, ';').replace(/“|”/g, '"').replace(/‘|’/g, "'").replace(/(/g, '(').replace(/)/g, ')'), phA: '你好, world. 真的?' }) T({ id: 'tab-space-convert', name: 'Tab / 空格转换', group: 'format', icon: 'code', desc: 'Tab 与空格互换(2/4/8 空格都支持)', keywords: ['tab', 'space', '缩进'], ph: '\tif (a) {\n\t\treturn 1\n\t}', fn: (s) => `Tab → 2 空格:\n${s.replace(/\t/g, ' ')}\n\nTab → 4 空格:\n${s.replace(/\t/g, ' ')}\n\n4 空格 → Tab:\n${s.replace(/ {4}/g, '\t')}` }) T({ id: 'linebreak-convert', name: '换行符转换', group: 'format', icon: 'code', desc: 'CRLF / LF / CR 互转,也能把 \\n 字面量变成真换行', keywords: ['linebreak', 'crlf', 'lf', '换行'], ph: '第一行\n第二行', fn: (s) => `LF:${s.replace(/\r\n?/g, '\n').split('').length} 字节\nCRLF 版本(复制走):\n${s.replace(/\r?\n/g, '\r\n')}\n\n字面量 \\n → 真换行:\n${s.replace(/\\n/g, '\n')}` }) T({ id: 'zero-width-check', name: '零宽字符检测', group: 'encode', icon: 'shield', desc: '找出藏起来的零宽字符,也能一键清掉', keywords: ['zero width', '零宽', '隐形'], ph: '粘一段可疑文本…', fn: (s) => { const bad = Array.from(s).filter((c) => /[\u200b-\u200f\u2060\ufeff]/.test(c)) return `发现 ${bad.length} 个零宽字符\n清理后:${s.replace(/[\u200b-\u200f\u2060\ufeff]/g, '')}` } }) T({ id: 'zero-width-steg', name: '零宽隐写', group: 'crypto', icon: 'shield', desc: '把短消息藏进零宽字符里,肉眼完全看不出来', keywords: ['steganography', '隐写', '零宽'], ph: '要藏起来的短句(会生成一段带隐藏信息的文本)', fn: (s) => { const bit = (c) => c === '1' ? '\u200b' : '\u200c' return '把下面这段整段复制走,外人只看得到空格:\n\n' + Array.from(enc(s)).map((b) => ' ' + b.toString(2).padStart(8, '0').split('').map(bit).join('')).join('') } }) G({ id: 'password-generator', name: '密码生成器', group: 'crypto', icon: 'key', desc: '按长度与字符集生成强密码,可选易读模式', keywords: ['password', '密码', '随机'], fields: [ { key: 'len', label: '长度', type: 'number', value: 16, min: 6, max: 64 }, { key: 'set', label: '字符集', type: 'select', value: 'all', options: [['all', '大小写+数字+符号'], ['alnum', '大小写+数字'], ['lower', '小写+数字'], ['pin', '纯数字']] }, { key: 'n', label: '数量', type: 'number', value: 5, min: 1, max: 50 }], run: ({ len, set, n }) => { const sets = { all: 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789!@#$%^&*-_=+?', alnum: 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789', lower: 'abcdefghijkmnopqrstuvwxyz23456789', pin: '0123456789' } const pool = sets[set]; const out = [] for (let k = 0; k < n; k++) { const a = new Uint32Array(len); crypto.getRandomValues(a); out.push(Array.from(a, (x) => pool[x % pool.length]).join('')) } return { text: out.join('\n'), hint: `已生成 ${n} 个,${len} 位` } } }) G({ id: 'passphrase-generator', name: '口令短语生成', group: 'crypto', icon: 'key', desc: '几个好记的词拼起来,比乱码好记还更安全', keywords: ['passphrase', '口令', '好记'], fields: [ { key: 'n', label: '词数', type: 'number', value: 4, min: 3, max: 10 }, { key: 'sep', label: '连接符', type: 'select', value: '-', options: [['-', '短横'], ['_', '下划线'], ['.', '点'], [' ', '空格']] }, { key: 'num', label: '加数字', type: 'check', value: true }], run: ({ n, sep, num }) => { const W = '潮水 山峰 河马 灯塔 黄油 松树 风筝 贝壳 铁锅 走廊 兔子 钥匙 云朵 石桥 铁道 铃铛 沙滩 银针 橘子 玻璃 木马 燕子 灯笼 雪松 火柴 木桶 风筝 布袋 皮球 铜锣'.split(' ') const pick = () => W[Math.floor(Math.random() * W.length)] const a = Array.from({ length: n }, pick); if (num) a[Math.floor(Math.random() * a.length)] += Math.floor(Math.random() * 90 + 10) return a.join(sep) } }) T({ id: 'password-strength', name: '密码强度评估', group: 'crypto', icon: 'shield', desc: '看长度、字符种类、常见弱密码,估算破解耗时', keywords: ['strength', '强度', '弱密码'], ph: '输入一个密码试试', fn: (s) => { if (!s) return '输入密码后这里显示评估结果' const weak = ['123456', 'password', 'qwerty', '111111', '12345678', 'abc123', 'admin', 'iloveyou', '666666', '888888', 'a123456'] const sets = [/[a-z]/, /[A-Z]/, /\d/, /[^a-zA-Z0-9]/].filter((r) => r.test(s)).length const pool = [/[a-z]/.test(s) ? 26 : 0, /[A-Z]/.test(s) ? 26 : 0, /\d/.test(s) ? 10 : 0, /[^a-zA-Z0-9]/.test(s) ? 33 : 0].reduce((a, b) => a + b, 0) const bits = Math.round(s.length * Math.log2(pool || 1)) const score = Math.min(100, Math.round(bits / 1.2)) const secs = Math.pow(2, bits) / 1e10 const human = secs < 60 ? `${secs.toFixed(0)} 秒` : secs < 3600 ? `${(secs / 60).toFixed(0)} 分钟` : secs < 86400 ? `${(secs / 3600).toFixed(1)} 小时` : secs < 3.15e7 ? `${(secs / 86400).toFixed(0)} 天` : `${(secs / 3.15e7).toExponential(1)} 年` return `评分:${score}/100(比特 ${bits})\n字符种类:${sets} 类,长度 ${s.length}\n弱密码表命中:${weak.includes(s.toLowerCase()) ? '是 ⚠︎' : '否'}\n按每秒 100 亿次估算需要:${human}` } }) G({ id: 'uuid-generator', name: 'UUID 生成器', group: 'crypto', icon: 'hash', desc: '批量生成 UUID v4,也能生成短 ID', keywords: ['uuid', 'guid', '唯一'], fields: [{ key: 'n', label: '数量', type: 'number', value: 5, min: 1, max: 100 }, { key: 'mode', label: '格式', type: 'select', value: 'v4', options: [['v4', 'UUID v4'], ['nodash', '去掉短横'], ['short', '短 ID(12 位)']] }], run: ({ n, mode }) => Array.from({ length: n }, () => { const u = crypto.randomUUID(); return mode === 'nodash' ? u.replace(/-/g, '') : mode === 'short' ? u.replace(/-/g, '').slice(0, 12) : u }).join('\n') }) T({ id: 'md5-text', name: 'MD5 校验值', group: 'crypto', icon: 'hash', desc: '浏览器里算 MD5(十六进制 / Base64),校验文件小众场景够用', keywords: ['md5', '校验', 'hash'], ph: '要算 MD5 的文本', fn: (s) => { const K = new Uint32Array(64), S = [7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21] for (let i = 0; i < 64; i++) K[i] = Math.floor(Math.abs(Math.sin(i + 1)) * 4294967296) const bytes = enc(s); const len = bytes.length; const withPad = (((len + 8) >> 6) + 1) * 64 const buf = new Uint8Array(withPad); buf.set(bytes); buf[len] = 0x80 const dv = new DataView(buf.buffer); dv.setUint32(withPad - 8, len * 8, true) let a0 = 0x67452301, b0 = 0xefcdab89, c0 = 0x98badcfe, d0 = 0x10325476 for (let off = 0; off < withPad; off += 64) { const M = new Uint32Array(16); for (let i = 0; i < 16; i++) M[i] = dv.getUint32(off + i * 4, true) let A = a0, B = b0, C = c0, D = d0 for (let i = 0; i < 64; i++) { let F, g if (i < 16) { F = (B & C) | (~B & D); g = i } else if (i < 32) { F = (D & B) | (~D & C); g = (5 * i + 1) % 16 } else if (i < 48) { F = B ^ C ^ D; g = (3 * i + 5) % 16 } else { F = C ^ (B | ~D); g = (7 * i) % 16 } const tmp = D; D = C; C = B; B = (B + ((A + F + K[i] + M[g]) | 0) | 0); B = (B << S[i]) | (B >>> (32 - S[i])); B = (B + tmp) | 0; A = tmp } a0 = (a0 + A) | 0; b0 = (b0 + B) | 0; c0 = (c0 + C) | 0; d0 = (d0 + D) | 0 } const out = new Uint8Array(16); const ov = new DataView(out.buffer) ;[a0, b0, c0, d0].forEach((v, i) => ov.setUint32(i * 4, v >>> 0, true)) return `MD5:${hex(out)}\nBase64:${btoa(String.fromCharCode(...out))}` } }) })()