Keats / rust-bcrypt

Easily hash and verify passwords using Bcrypt
MIT License
340 stars 49 forks source link

Add Clone and Debug traits to Version enum #84

Closed sn99 closed 6 months ago

sn99 commented 6 months ago

@Keats tell me if you want me to add this:

// Rewrite of bcrypt.cc for decode_base64
const INDEX_64: [u8; 128] = [
    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
    255, 255, 255, 255, 255, 255, 255, 255, 0, 1, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 255, 255,
    255, 255, 255, 255, 255, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
    21, 22, 23, 24, 25, 26, 27, 255, 255, 255, 255, 255, 255, 28, 29, 30, 31, 32, 33, 34, 35, 36,
    37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 255, 255, 255, 255, 255,
];

fn char64(c: u8) -> u8 {
    if c > 127 {
        255
    } else {
        INDEX_64[c as usize]
    }
}

pub fn decode_base64(buffer: &mut [u8], len: usize, data: &[u8]) {
    let mut bp = 0;
    let mut p = 0;
    while bp < len {
        let c1 = char64(data[p]);
        let c2 = char64(data[p + 1]);

        /* Invalid data */
        if c1 == 255 || c2 == 255 {
            break;
        }

        buffer[bp] = (c1 << 2) | ((c2 & 0x30) >> 4);
        bp += 1;
        if bp >= len {
            break;
        }

        let c3 = char64(data[p + 2]);
        if c3 == 255 {
            break;
        }

        buffer[bp] = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2);
        bp += 1;
        if bp >= len {
            break;
        }

        let c4 = char64(data[p + 3]);
        if c4 == 255 {
            break;
        }
        buffer[bp] = ((c3 & 0x03) << 6) | c4;
        bp += 1;

        p += 4;
    }
}

Mainly because salt in hash_with_salt is bounded to [u8;16] but in original it can be any length ans uses above func to resize it.

Keats commented 6 months ago

I think it's fine to keep the base64 dep

sn99 commented 6 months ago

I think it's fine to keep the base64 dep

Oops, it seems I was wrong, the original brycpt.cc just ignore the rest after 16 bytes so my code is not needed.

Can we merge the debug and clone?