DEV Community

lionel-rowe
lionel-rowe

Posted on

2

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)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay