Q:

SHA256 different hash in nodejs and kotlin

I am using a SHA 256 hash generator in two different modules. One module is written in Kotlin and the other one using the nodejs crypto API. The hashed value is slightly different in nodejs and kotlin. 
Kotlin Code
import java.security.MessageDigest
import java.util.Base64
fun main() {
        val md = MessageDigest.getInstance("SHA-256")
        val input = "test".toByteArray(Charsets.UTF_8)
        val bytes = md.digest(input)
        println(Base64.getUrlEncoder().encodeToString(bytes))
}

Nodejs code
const crypto = require('crypto');
const hash = crypto.createHash('sha256');

hash.update('test');
console.log(crypto.createHash('sha256').update('test', 'utf8').digest('base64'));

The kotlin code outputs n4bQgYhMfWWaL-qgxVrQFaO_TxsrC4Is0V1sFbDwCgg= where as the nodejs code outputs n4bQgYhMfWWaL+qgxVrQFaO/TxsrC4Is0V1sFbDwCgg=. I couldn't identify what i am doing wrong?

A:

I believe your issue is caused by the urlencoded base64 provided by kotlin: Base64.getUrlEncoder().
In kotlin you are getting the base64 url encoded, while at node you only got base64.
Maybe this helps you to determine if the assumption is correct.
Add the url encoding at node:
// https://www.npmjs.com/package/base64url

const base64url = require('base64url');
console.log(base64url(crypto.createHash('sha256').update('test', 'utf8')));

