aboutsummaryrefslogtreecommitdiff
path: root/src/stable/crypt.cpp
blob: 0dbe2d55ad5e62d105fbf90802e39a9194ccb8a0 (plain)
1
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
 * Copyright (C) 2007-2019 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include "bu/crypt.h"
#include "bu/md5.h"
#include "bu/base64.h"
#include "bu/membuf.h"
#include "bu/file.h"

Bu::String Bu::cryptPass( const Bu::String &sPass, const Bu::String &sSalt )
{
    Bu::Md5 md5;
    Bu::MemBuf mbOut;
    Bu::Base64 b64Out( mbOut );
    
    Bu::String::const_iterator i = sSalt.find('$');
    Bu::String sSaltSml = sSalt.getSubStr( sSalt.begin(), i );

    md5.addData( sPass );
    md5.addData( sSaltSml );
    md5.writeResult( b64Out );

    b64Out.stop();

    return sSaltSml + "$" + mbOut.getBlob();
}

Bu::String Bu::cryptPass( const Bu::String &sPass )
{
    Bu::MemBuf mbSalt;
    Bu::Base64 b64Salt( mbSalt );
    Bu::File fRand("/dev/urandom", Bu::File::Read );

#define STR 6
    char buf[STR];
    fRand.read( buf, STR );
    b64Salt.write( buf, STR );

    b64Salt.stop();

    return cryptPass( sPass, mbSalt.getBlob() );
}