Skip to content

Instantly share code, notes, and snippets.

@fenying
fenying / add-apache2-copyright.sh
Created November 3, 2023 10:53
Add Apache2 Copyright info to files
@fenying
fenying / pbkdf2.js
Created March 5, 2023 10:33
PBKDF2 implementation in JS
// A JS implementation of PBKDF2.
// For study only, don't use this.
// Use NodeJS native implementation `crypto.pbkdf2` instead.
const Crypto = require('node:crypto');
function hmac(hmacAlgo, key, data) {
return Crypto.createHmac(hmacAlgo, key).update(data).digest();
}
const BUF_INT32_BE = Buffer.alloc(4);
@fenying
fenying / request-wepay-sandbox-key.ts
Created May 26, 2021 11:31
Request wechat pay sandbox api sign key.
import * as $crypto from 'crypto';
const WEPAY_MCH_ID = 12345678;
const WEPAY_API_KEY = '';
const NONCE_STR = $crypto.randomBytes(16).toString('hex');
const REQ_SIGN = $crypto.createHash('md5')
.update(`mch_id=${WEPAY_MCH_ID}&nonce_str=${NONCE_STR}&sign_type=MD5&key=${WEPAY_API_KEY}`)
.digest('hex')
@fenying
fenying / clean-docker-logs.sh
Created May 8, 2021 02:47
Clean up all docker container logs
#!/bin/sh
## See https://stackoverflow.com/a/48029079/6559859
for k in $(docker ps --format='{{.ID}}')
do
echo "" > $(docker inspect --format='{{.LogPath}}' $k)
done
@fenying
fenying / docker-mongo.sh
Created January 20, 2020 10:32
Deploy MongoDB inside docker.
#!/bin/bash
MONGO_DOCKER_MAX_MEMORY=512M
MONGO_DOCKER_PORT=27017
MONGO_DOCKER_DATA_DIR=/data/db
MONGO_DOCKER_NAME=Mongo-Server
MONGO_LOCAL_PORT=27017
MONGO_LOCAL_DATA_DIR=/docker/mongo/db
@fenying
fenying / calcWeaponScroll.ts
Created November 24, 2019 09:47
[MapleStory] Calculate the average ATTACK added in a weapon by scrolls
/**
* Calcuate the used scrolls of a weapon.
*
* @param baseAttack The base attack
* @param totalAttack The total attack
* @param sparkAttack The ATTACK added by spark
* @param appliedScrolls The quantity of scrolls applied.
* @param appliedStars The quantity of stars applied.
*/
@fenying
fenying / docker-rabbitmq.sh
Last active October 23, 2023 02:20
Deploy RabbitMQ server with docker.
MQ_DOCKER_PORT=5672
MQ_DOCKER_ADMIN_USER="user"
MQ_DOCKER_ADMIN_PASS="password"
MQ_DOCKER_ADMINCP_PORT=15672
MQ_DOCKER_PATH=/var/lib/rabbitmq
MQ_DOCKER_NAME="RabbitMQ-Server"
MQ_DOCKER_MEMORY=256M
MQ_LOCAL_PATH=/docker/rabbitmq
MQ_LOCAL_HOST=0.0.0.0
@fenying
fenying / docker-php-fpm.sh
Created September 24, 2019 03:44
Install PHP-FPM by docker with extensions for MySQL and Redis
FROM php:fpm
RUN pecl install -o -f redis \
&& rm -rf /tmp/pear \
&& docker-php-ext-enable redis \
&& docker-php-ext-install mysqli pdo pdo_mysql
CMD ["php-fpm"]
@fenying
fenying / calcIgnoreMobDefnese.js
Last active August 12, 2019 08:11
[MapleStory] Calculate ignore ratio of monster defense.
/**
* Calculate the ignore ratio of monster defense.
*
* @param base The base ignore ratio, (Value: 0 ~ 1)
* @param ignoreItems The items of ignore addition to be added, (Value: 0 ~ 1)
*/
function calcIgnoreMobDefnese(base, ignoreItems) {
return Math.floor(ignoreItems.reduce((p, v) => p + (1 - p) * v, base) * 100) / 100;
}
@fenying
fenying / git-switch-remote-repo.sh
Last active April 24, 2019 02:28
A tool for switching git remote repository URL.
#!/bin/bash
# Usage
#
# We assume the link of remote repository is of SSH style like:
#
# <git-user>@<domain>:<scope>/<project-name>.git
#
# git-switch-remote-repo <remote-name> full <full-repo-link>
# git-switch-remote-repo <remote-name> domain <new-domain>