Skip to content

Instantly share code, notes, and snippets.

@righ
righ / pbkdf2.js
Last active Aug 1, 2020
pbkdf2 password hasher like django
View pbkdf2.js
const { pbkdf2Sync } = require('crypto');
export const validate = (raw, password, len=32) => {
const [method, iterations, salt, hash] = password.split('$');
const digest = method.split("_")[1];
return pbkdf2Sync(raw, salt, parseInt(iterations, 10), len, digest).toString('base64') === hash;
};
export const make = (raw, iterations, digest="sha256", salt="", len=32) => {
if (salt === "") {
@righ
righ / arraytools.ts
Last active Jun 16, 2020
Array tools in TypeScript
View arraytools.ts
export const range = (start: number, stop: number, step: number=1) => {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#Sequence_generator_(range)
return Array.from({ length: (stop - start - 1) / step + 1}, (_, i) => start + (i * step));
}
export const all = (values: any[]) => {
for (let value of values) {
if (!value) {
return false;
}
@righ
righ / zigzag_sorted.py
Created Jun 8, 2019
It returns list sorted in zigzag.
View zigzag_sorted.py
def zigzag_sorted(iterable, key=None):
"""It returns list sorted in zigzag.
:rtype: generator object (iterator)
>>> list(zigzag_sorted([1, 2, 3, 4, 5, 6, 7]))
[1, 7, 2, 6, 3, 5, 4]
"""
i = 0
l = sorted(iterable, key=key)
n = len(l)
View time_to_seconds.py
import re
DEFAULT_MULTIPLIERS = {
'S': 1,
'M': 60,
'H': 60 * 60,
'd': 60 * 60 * 24,
}
def time_to_seconds(pattern, target, multipliers={}):
@righ
righ / escape.js
Created Aug 6, 2018
markdown_xss_test
View escape.js
const deniedTagCondition = /^<\/?(script|style|link|iframe|embed|object|html|head|meta|body|form|input|button)/i
const deniedAttrCondition = /^(on.+|style|href|action|id|class|data-.*)/i
const escape = (txt) => {
if (txt.match(deniedTagCondition) || txt.indexOf('<!') === 0 || txt.indexOf('<?') === 0 || txt.indexOf('<\\') === 0) { return '' }
if (txt.indexOf('</') === 0) { return txt }
let outer = document.createElement('div')
outer.innerHTML = txt
let el = outer.querySelector('*')
if (!el) {return ''}
@righ
righ / checkboxWidget.jsx
Last active Jul 12, 2018
React customized checkbox
View checkboxWidget.jsx
import React from 'react'
import './checkboxWidget.styl'
let defaultStyle = {width: 22, height: 22}
const CheckBox = (props) => {
let onClick = () => !props.readonly && !props.disabled ? props.handleChange() : null
let borderColor = props.borderColor ? props.borderColor : '#aaa'
let backgroundColor = props.checked ? props.backgroundColor ? props.backgroundColor : '#transparent' : 'transparent'
let style = Object.assign({}, defaultStyle, props.style)
View excel_column_converter.py
from string import ascii_uppercase
def to_column(number):
"""
:param int number: 1以上の数値
"""
string = ''
while True:
number -= 1
View format_timezones.py
#!/usr/bin/env python
# coding: utf-8
import sys
def format_timezones(timezones, descriptor=sys.stdout):
_tz = ()
for tz in sorted(t.split('/') for t in timezones):
for indent, region in enumerate(tz):
if len(_tz) > indent and _tz[indent] == tz[indent]:
View dtformat_regexp.py
# coding: utf-8
import re
_02 = (lambda s: '{0:0>2}'.format(s))
_03 = (lambda s: '{0:0>3}'.format(s))
MAP = {
'%a': r'(Sun|Mon|Tue|Wed|Thu|Fri|Sat)',
'%A': r'(Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday)',
'%w': r'([0-6]{1})',
View only.py
#!/usr/bin/env python
# coding: utf-8
import socket
import functools
def nop():
"""no operation"""
You can’t perform that action at this time.