Skip to content

Instantly share code, notes, and snippets.

View TransparentLC's full-sized avatar
🐟
Touching fish _(:зゝ∠)_

✨小透明・宸✨ TransparentLC

🐟
Touching fish _(:зゝ∠)_
View GitHub Profile
@TransparentLC
TransparentLC / chacha20poly1305-test.js
Created June 27, 2021 05:25
4 KB 左右的 ChaCha20Poly1305 认证加密算法实现,参考 https://github.com/thesimj/js-chacha20https://github.com/devi/chacha20poly1305 进行了少量修改。
const { ChaCha20Poly1305 } = require('./chacha20poly1305.min.js');
const hexToBytes = e => new Uint8Array(e.match(/[0-9a-f]{2}/gi).map(e => parseInt(e, 16)));
const bytesToHex = e => Array.from(e).map(e => e.toString(16).padStart(2, 0)).join('');
// Test vectors from:
// https://datatracker.ietf.org/doc/html/rfc7539#section-2.8.2
const keyC = hexToBytes('808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f')
const nonceC = hexToBytes('070000004041424344454647');
@TransparentLC
TransparentLC / ed25519-test.js
Last active June 25, 2021 11:58
10 KB 左右的 Ed25519 签名和验证算法实现,在 https://github.com/paulmillr/noble-ed25519 的基础上添加了 IIFE,将使用的 SHA-512 从 Web Crypto API 替换成 https://github.com/emn178/js-sha512 从而实现同步调用,并进行了部分修改以减小 minify 后的大小。
const Ed25519 = require('./ed25519.min.js');
const hexToBytes = e => new Uint8Array(e.match(/[0-9a-f]{2}/gi).map(e => parseInt(e, 16)));
const bytesToHex = e => Array.from(e).map(e => e.toString(16).padStart(2, 0)).join('');
// Test vectors from:
// https://datatracker.ietf.org/doc/html/rfc8032#section-7.1
const privateA = hexToBytes('4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb');
const messageA = hexToBytes('72');
@TransparentLC
TransparentLC / x25519-test.js
Last active June 25, 2021 11:50
2 KB 左右的 X25519 密钥交换算法实现,在 https://github.com/CryptoEsel/js-x25519 的基础上添加了 IIFE,并进行了部分修改以减小 minify 后的大小。
const X25519 = require('./x25519.min.js');
const hexToBytes = e => new Uint8Array(e.match(/[0-9a-f]{2}/gi).map(e => parseInt(e, 16)));
const bytesToHex = e => Array.from(e).map(e => e.toString(16).padStart(2, 0)).join('');
// Test vector from:
// https://datatracker.ietf.org/doc/html/rfc7748.html#section-6.1
const privateA = hexToBytes('77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a');
const privateB = hexToBytes('5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb');
const publicA = X25519.getPublic(privateA);
@TransparentLC
TransparentLC / base85.js
Created May 7, 2021 10:16
Base85 编码和解码
const charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~';
const charsetReverse = charset
.split('')
.reduce((acc, cur, idx) => {
acc[cur] = idx;
return acc;
}, {});
/**
* @param {Uint8Array} data
@TransparentLC
TransparentLC / perm.js
Last active April 26, 2021 01:23
生成置换和原地求逆置换
const generatePerm = (len, rng) => {
const result = new Array(len);
for (let i = 0; i < len; i++) {
result[i] = i;
}
for (let i = len - 1; i >= 0; i--) {
const j = rng() % (i + 1);
[result[i], result[j]] = [result[j], result[i]];
}
return result;
@TransparentLC
TransparentLC / dummy-file.py
Last active March 22, 2021 15:56
使用多进程生成随机数据填充的文件。
import argparse
import multiprocessing
import re
import os
import sys
import time
def parseFileSize(string: str) -> int:
try:
size, unit = re.search(r'^([0-9]*\.?[0-9]+)((?:b|kb|mb|gb)?)$', (string if type(string) is str else str(string)).lower()).groups()
@TransparentLC
TransparentLC / xoshiro128ss.py
Created March 12, 2021 16:26
可播种的随机数生成器 xoshiro128**。如果只是为了生成随机 uint32 整数的话,Python 自带的 random.randint、random.randbytes 和 os.urandom 比它都快上不少。
import ctypes
class Xoshiro128ss:
def __init__(self, a: int, b: int, c: int, d: int):
self.s = tuple(ctypes.c_uint32(x) for x in (a, b, c, d))
self.t = ctypes.c_uint32()
self.r = ctypes.c_uint32()
def randInt(self) -> int:
s0, s1, s2, s3 = self.s
#!/usr/bin/env pwsh
# https://stackoverflow.com/questions/8761888/capturing-standard-out-and-error-with-start-process
function Start-Command ([String]$Path, [String]$Arguments) {
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = $Path
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$pinfo.Arguments = $Arguments
@TransparentLC
TransparentLC / icas-login.py
Last active December 27, 2020 07:26
ICAS模拟登录,可以用于对 https://jwxk.jnu.edu.cn/ 等各种使用 ICAS 进行统一登录的系统的自动化操作。
import getpass
import pydes
import re
import requests
username = input('Username: ')
password = getpass.getpass('Password: ')
session = requests.Session()
response = session.get('https://icas.jnu.edu.cn/cas/login')
@TransparentLC
TransparentLC / phar-builder.php
Created December 10, 2020 03:25
并没有什么用的 PHAR 打包脚本
<?php
/*
PHAR打包脚本
使用方法:
php phar-builder.php /path/to/build/config.json
打包配置文件:
{
// 项目名称,也是打包的文件名称