Comparing BCrypt hash between PHP and NodeJS

后端 未结 3 1709
梦毁少年i
梦毁少年i 2020-12-04 15:04

For an app I\'m working on, nodejs needs to verify hashes created by PHP and vice-versa.

The problem is, the hashes generated in PHP (via Laravel\'s Hash

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-04 15:26

    The implementation of bcrypt in different language might be differ.

    For example, in Node.js version bcrypt.js, the salt length applied are 29 characters

        bcrypt.getSalt = function(hash) {
            if (typeof hash !== 'string')
                throw Error("Illegal arguments: "+(typeof hash));
            if (hash.length !== 60)
                throw Error("Illegal hash length: "+hash.length+" != 60");
            return hash.substring(0, 29);
        };
    

    But, in Go version golang.org/x/crypto/bcrypt, the salt size are 22 of bytes:

    const (
        majorVersion       = '2'
        minorVersion       = 'a'
        maxSaltSize        = 16
        maxCryptedHashSize = 23
        encodedSaltSize    = 22
        encodedHashSize    = 31
        minHashSize        = 59
    )
    

    So, it might happen that hashed string in Node.js gets error when compared in Go, other languages likewise.

提交回复
热议问题