lsp-yggdrasil/src/generator.js

35 lines
1.2 KiB
JavaScript

import * as Crypto from 'crypto'
import hexToUuid from 'hex-to-uuid'
export function generateToken(username) {
const key = `${Date.now()}-${ Crypto.createHash('sha256').update(Crypto.randomBytes(32)).digest('hex')}-${username}-lsp-${Math.random() * 1.048596}`
const token = Crypto.createHash('sha256').update(key).digest('hex')
return [token, key]
}
export function uuid(input) {
let md5Bytes = Crypto.createHash('md5').update(input).digest()
md5Bytes[6] &= 0x0f; // clear version
md5Bytes[6] |= 0x30; // set to version 3
md5Bytes[8] &= 0x3f; // clear variant
md5Bytes[8] |= 0x80; // set to IETF variant
return hexToUuid(md5Bytes.toString('hex'))
}
export function noSymboUUID(input) {
let md5Bytes = Crypto.createHash('md5').update(input).digest()
md5Bytes[6] &= 0x0f; // clear versio\n
md5Bytes[6] |= 0x30; // set to version 3
md5Bytes[8] &= 0x3f; // clear variant
md5Bytes[8] |= 0x80; // set to IETF variant
return md5Bytes.toString('hex')
}
export function uuidToNoSymboUUID(uuid) {
return uuid.replace(/-/g, '')
}
export function toSymboUUID(uuid) {
return hexToUuid(uuid)
}