lsp-yggdrasil/src/generator.js

35 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

2022-05-02 15:12:46 +00:00
import * as Crypto from 'crypto'
import hexToUuid from 'hex-to-uuid'
2022-05-02 15:12:46 +00:00
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]
}
2022-05-02 15:12:46 +00:00
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()
2022-05-03 11:05:28 +00:00
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) {
2022-05-03 11:05:28 +00:00
return uuid.replace(/-/g, '')
}
export function toSymboUUID(uuid) {
return hexToUuid(uuid)
2022-05-03 11:38:12 +00:00
}