DEV Community

lionel-rowe
lionel-rowe

Posted on

NodeJS `Buffer` is cool

function displayBufferContents(buf) {
    const byteString = Array.from(new Uint8Array(buf.buffer), (byte) =>
        String.fromCharCode(byte),
    ).join('')

    return byteString.replaceAll(/\u0000+/g, (m) => `{...${m.length}}`)
}

const secretKeyBuffer = Buffer.from('SECRET KEY PLEASE DO NOT STEAL')

displayBufferContents(Buffer.from('arbitrary text'))
// /{...7}/{...7}SECRET KEY PLEASE DO NOT STEAL{...2}arbitrary text{...8130}
Enter fullscreen mode Exit fullscreen mode

cool


Credit to @paulmillr on GitHub:

For the record, there are 800+ public code cases on GH that use Buffer.from(privateKey). Imagine how private repositories look like and what other secrets are stored there (symmetric keys, etc)

https://github.com/search?q=Buffer.from%28privateKey&type=code

Top comments (0)