mashingan / anonimongo

Another Nim pure Mongo DB driver
MIT License
43 stars 5 forks source link

Type error when using SCRAM SHA 256 #20

Closed samsamros closed 1 year ago

samsamros commented 1 year ago

Using the example on site:

import std/net
import anonimongo

var mongo = newMongo[Socket]()
if not mongo.connect:
  quit "Cannot connect to localhost:27017"

if not mongo.authenticate[:SHA256Digest](username, password): #credentials specified with var on my code
  quit "Cannot login to localhost:27017"
close mongo

I get the following error running a vanilla compilation on version 1.6.10 and 1.7, no --run, just -d:ssl

Error: type mismatch: got 'char' for 'char(int32(result[xgensym75]) xor int32(previous[xgensym75]))' but expected 'uint8'

I traced it back to the template in scram/private/utils.nim in the scram code:

template `^=`*[T](a, b: T) =
  for x in 0..<a.len:
    when T is Sha1Digest:
      a[x] = (a[x].int32 xor b[x].int32).uint8
    else:
      a[x] = (a[x].int32 xor b[x].int32).char

and changed it to:

template `^=`*[T](a, b: T) =
  for x in 0..<a.len:
    when T is Sha1Digest:
      a[x] = (a[x].int32 xor b[x].int32).uint8
    else:
      a[x] = (a[x].int32 xor b[x].int32).uint8 # <- changed char to uint8

and the code finally ran without any issues in my case. I'm not sure if this error has occured to other users, or if I need to update some package, but this did the trick for me. The thing is it ends up being the same process as SHA1, so I'm not sure this is intended or if there's another solution. Please let me know if this helps, and thanks for the amazing work you have put into this code. I use this package a lot, and I think it is wonderful.

samsamros commented 1 year ago

nevermind! I had not updated to scram 0.2. The code works and it is implemented as uint8. In version 0.2. the template looks like:

template `^=`*[T](a, b: T) =
  for x in 0..<a.len:
    when T is Sha1Digest or T is Keccak512Digest or T is SHA256Digest:
      a[x] = (a[x].int32 xor b[x].int32).uint8
    else:
      a[x] = (a[x].int32 xor b[x].int32).char

So SHA256 also ends up being uint8, and that's why my fix for the other code worked. I hope it helps other users!

mashingan commented 1 year ago

I think Anonimongo needs to upgrade its dependency's version.