Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
linux password sha512 encrypt with nodejs
const crypto = require('crypto')
const SALT_LEN_MAX = 16
const DEFAULT_ROUNDS = 5000
function sha512crypt(key, salt) {
salt = salt.slice(0, SALT_LEN_MAX)
let cnt = 0
// step 1-3
const hashA = crypto.createHash('sha512')
hashA.update(key)
hashA.update(salt)
// step 4-8
const hashB = crypto.createHash('sha512')
hashB.update(key)
hashB.update(salt)
hashB.update(key)
let altResult = hashB.digest()
// step: 9-10
for (cnt = key.length; cnt > 64; cnt -= 64) {
hashA.update(altResult)
}
hashA.update(altResult.slice(0, cnt))
// setp: 11-12
for (cnt = key.length; cnt > 0; cnt >>= 1) {
if ((cnt & 1) != 0) {
hashA.update(altResult)
} else {
hashA.update(key)
}
}
altResult = hashA.digest()
// step: 13-15
const hashDP = crypto.createHash('sha512')
for (cnt = 0; cnt < key.length; cnt++) {
hashDP.update(password)
}
let tempResult = hashDP.digest()
// step: 16
let P = Buffer.from('')
for (cnt = key.length; cnt > 64; cnt -= 64) {
P = Buffer.concat([P, tempResult])
}
P = Buffer.concat([P, tempResult.slice(0, cnt)])
// step: 17-19
const hashDS = crypto.createHash('sha512')
for (cnt = 0; cnt < 16 + altResult[0]; ++cnt) hashDS.update(salt)
tempResult = hashDS.digest()
// step: 20
let S = Buffer.from('')
for (cnt = salt.length; cnt >= 64; cnt -= 64) {
S = Buffer.concat([S, tempResult])
}
S = Buffer.concat([S, tempResult.slice(0, cnt)])
// step: 21
for (cnt = 0; cnt < DEFAULT_ROUNDS; ++cnt) {
const hashC = crypto.createHash('sha512')
if ((cnt & 1) != 0) {
hashC.update(P.slice(0, key.length))
} else {
hashC.update(altResult)
}
if (cnt % 3 != 0) {
hashC.update(S.slice(0, salt.length))
}
if (cnt % 7 != 0) {
hashC.update(P.slice(0, key.length))
}
if ((cnt & 1) != 0) {
hashC.update(altResult)
} else {
hashC.update(P.slice(0, key.length))
}
altResult = hashC.digest()
}
// step: 22
const result = [
radix64(Buffer.from([altResult[0], altResult[21], altResult[42]])),
radix64(Buffer.from([altResult[22], altResult[43], altResult[1]])),
radix64(Buffer.from([altResult[44], altResult[2], altResult[23]])),
radix64(Buffer.from([altResult[3], altResult[24], altResult[45]])),
radix64(Buffer.from([altResult[25], altResult[46], altResult[4]])),
radix64(Buffer.from([altResult[47], altResult[5], altResult[26]])),
radix64(Buffer.from([altResult[6], altResult[27], altResult[48]])),
radix64(Buffer.from([altResult[28], altResult[49], altResult[7]])),
radix64(Buffer.from([altResult[50], altResult[8], altResult[29]])),
radix64(Buffer.from([altResult[9], altResult[30], altResult[51]])),
radix64(Buffer.from([altResult[31], altResult[52], altResult[10]])),
radix64(Buffer.from([altResult[53], altResult[11], altResult[32]])),
radix64(Buffer.from([altResult[12], altResult[33], altResult[54]])),
radix64(Buffer.from([altResult[34], altResult[55], altResult[13]])),
radix64(Buffer.from([altResult[56], altResult[14], altResult[35]])),
radix64(Buffer.from([altResult[15], altResult[36], altResult[57]])),
radix64(Buffer.from([altResult[37], altResult[58], altResult[16]])),
radix64(Buffer.from([altResult[59], altResult[17], altResult[38]])),
radix64(Buffer.from([altResult[18], altResult[39], altResult[60]])),
radix64(Buffer.from([altResult[40], altResult[61], altResult[19]])),
radix64(Buffer.from([altResult[62], altResult[20], altResult[41]])),
radix64(Buffer.from([0, 0, altResult[63]])).slice(0, 2),
].join('')
return result
}
const radix64Encoder =
'./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
function radix64(buffer) {
let v = (buffer[0] << 16) | (buffer[1] << 8) | buffer[2]
const c1Idx = v & 0x3f
v = v >> 6
const c2Idx = v & 0x3f
v = v >> 6
const c3Idx = v & 0x3f
v = v >> 6
const c4Idx = v & 0x3f
v = v >> 6
return (
radix64Encoder[c1Idx] +
radix64Encoder[c2Idx] +
radix64Encoder[c3Idx] +
radix64Encoder[c4Idx]
)
}
const password = '123456'
const salt = 'PCjFvrAA/NnyKdMp'
const encryptPassword = sha512crypt(password, salt)
console.log(encryptPassword)
@isayme

This comment has been minimized.

Copy link
Owner Author

@isayme isayme commented Jan 5, 2021

# openssl passwd -6 -salt PCjFvrAA/NnyKdMp 123456
$6$PCjFvrAA/NnyKdMp$7fs0mn0nUuQ0jjtKZVAyf8TCBIx5MUvwC2ftkRwh2q7PYSuKpnv4wVu63zX.oCJ/RG2v4gDbNMCDAV1dIjCuE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment