Skip to content

Commit

Permalink
add trip
Browse files Browse the repository at this point in the history
  • Loading branch information
taisan11 committed Apr 27, 2024
1 parent 37b598a commit 9894212
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 2 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 2 additions & 0 deletions bunfig.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[install.scopes]
"@jsr" = "https://npm.jsr.io"
7 changes: 5 additions & 2 deletions src/module/KAS.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//@ts-ignore
import { createTripByKey } from "./trip";

function formatUnixTime(unixTime: number): string {
// UNIXタイムをミリ秒に変換
const date = new Date(unixTime * 1000);
Expand Down Expand Up @@ -71,8 +74,8 @@ async function NES(input: string, mail: string): Promise<{ name: string, mail: s
const convertedInput = input.replace(/[◆★\n]/g, function (m) { return map[m]; });
let trip = '';
if (length > 0) {
// trip = `◆`+await convertTrip(match, length, true);
trip = `◆` + `現在一時的にトリップは使用できません`
trip = `◆`+await createTripByKey(match[1]);
// trip = `◆` + `現在一時的にトリップは使用できません`
}
return { "name": `${convertedInput.replace(/#.*/, '')}${trip}`, "mail": mail }
}
53 changes: 53 additions & 0 deletions src/module/trip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import * as IconvCP932 from "iconv-cp932";

import { create10DigitsTrip, create12DigitsTrip, createRawKeyTrip } from './trip/index'

type Options = Partial<{
hideWhitespace: boolean
}>

const rawKeyPettern = /^#[0-9A-Fa-f]{16}[.\/0-9A-Za-z]{0,2}$/

const maskSpecialSymbols = (text: string) => text.replace(//g, '☆').replace(//g, '◇')

export const createTripByKey = (key: string) => {
// const encodedKeyString = convert(key, { from: 'UNICODE', to: 'SJIS', fallback: 'html-entity' })
const encodedKeyString = IconvCP932.encode(key).toString()

// 10 桁トリップ
if (encodedKeyString.length < 12) return create10DigitsTrip(key)

// 生キートリップ
if (encodedKeyString.startsWith('#') || encodedKeyString.startsWith('$')) {
// 拡張用のため ??? を返す
if (!rawKeyPettern.test(encodedKeyString)) return '???'

return createRawKeyTrip(key)
}

// 12 桁トリップ
return create12DigitsTrip(key)
}

export const createTripByText = (text: string, options?: Options) => {
const indexOfSharp = (() => {
const indexOfHalfWidthSharp = text.indexOf('#')
const indexOfFullWidthSharp = text.indexOf('#')

if (indexOfHalfWidthSharp >= 0) return indexOfHalfWidthSharp
if (indexOfFullWidthSharp >= 0) return indexOfFullWidthSharp

return -1
})()

if (indexOfSharp < 0) return maskSpecialSymbols(text)

const name = text.substr(0, indexOfSharp)
const key = text.substr(indexOfSharp + 1)

const whitespaceIfNeeded = options?.hideWhitespace ? '' : ' '

return `${maskSpecialSymbols(name)}${whitespaceIfNeeded}${createTripByKey(key)}`
}

export const createTrip = (text: string, options?: Options) => createTripByText(text, options)
41 changes: 41 additions & 0 deletions src/module/trip/10digits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { encode } from 'iconv-cp932'
import crypt from 'unix-crypt-td-js'

/**
* 10 桁トリップを生成する
*/
export const create10DigitsTrip = (key: string) => {
const saltSuffixString = 'H.'
// const encodedKeyString = convert(key, { from: 'UNICODE', to: 'SJIS', fallback: 'html-entity' })
const encodedKeyString = encode(key).toString()

const salt = `${encodedKeyString}${saltSuffixString}`
// 1 文字目から 2 文字を取得する
.substr(1, 2)
// . から z までの文字以外を . に置換する
.replace(/[^\.-z]/g, '.')
// 配列にする
.split('')
// salt として使えない記号をアルファベットに置換する
.map((string) => {
if (string === ':') return 'A'
if (string === ';') return 'B'
if (string === '<') return 'C'
if (string === '=') return 'D'
if (string === '>') return 'E'
if (string === '?') return 'F'
if (string === '@') return 'G'
if (string === '[') return 'a'
if (string === '\\') return 'b'
if (string === ']') return 'c'
if (string === '^') return 'd'
if (string === '_') return 'e'
if (string === '`') return 'f'

return string
})
// 文字列にする
.join('')

return (crypt(encodedKeyString, salt) as string).substr(-10, 10)
}
13 changes: 13 additions & 0 deletions src/module/trip/12digits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { encode } from 'iconv-cp932'
import { createHash } from 'node:crypto'

/**
* 12 桁トリップを生成する
*/
export const create12DigitsTrip = (key: string) => {
// const arrayBuffer = convert(key, { from: 'UNICODE', to: 'SJIS', type: 'arraybuffer', fallback: 'html-entity' })
const arrayBuffer = encode(key)
const byteArray = new Uint8Array(arrayBuffer)

return createHash('sha1').update(byteArray).digest().toString('base64').replace(/\+/g, '.').substr(0, 12)
}
3 changes: 3 additions & 0 deletions src/module/trip/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './10digits'
export * from './12digits'
export * from './raw'
26 changes: 26 additions & 0 deletions src/module/trip/raw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import crypt from 'unix-crypt-td-js'

/**
* 生キートリップを生成する
*/
export const createRawKeyTrip = (key: string) => {
const saltSuffixString = '..'

const rawKey = key
// 2 文字目以降の全ての文字列を取得
.substr(1)
// 2 文字ごとに配列に分割する
.match(/.{2}/g)!
// ASCII コードを ASCII 文字に変換する
.map((hexadecimalASCIICode) => {
const demicalASCIICode = parseInt(hexadecimalASCIICode, 16)

return String.fromCharCode(demicalASCIICode)
})
// 文字列にする
.join('')

const salt = `${key}${saltSuffixString}`.substr(17, 2)

return (crypt(rawKey, salt) as string).substr(-10, 10)
}

0 comments on commit 9894212

Please sign in to comment.