I followed the directions in the
Email with Postfix, Dovecot, and MySQL Linode Library guide several months ago and everything is working well so far. One thing I never fully understood was the encryption process used when setting user passwords. Here's the table schema:
Code:
CREATE TABLE `virtual_users` (
`id` int(11) NOT NULL auto_increment,
`domain_id` int(11) NOT NULL,
`password` varchar(106) NOT NULL,
`email` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`),
FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
and here is the query used to insert data:
Code:
INSERT INTO `mailserver`.`virtual_users`
(`id`, `domain_id`, `password` , `email`)
VALUES
('1', '1', ENCRYPT('firstpassword', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16))), 'email1@example.com'),
('2', '1', ENCRYPT('secondpassword', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16))), 'email2@example.com');
After doing some reading I think I have a very basic understanding of what's happening:
- A random number is generated
- A SHA1 hash is created from it
- The SUBSTRING function throws away the first 16 characters and keeps the rest of the hash string
- The remaining characters in the hash string are appended to the literal string '$6$'
- That combined value is used as the salt for the ENCRYPT function
- The resulting value is stored in the password field.
Assuming I have that right,
why is that password encryption routine (for the lack of a proper term) throwing away some of the characters? Is it so the resulting password will always fit in the
varchar(106) field?
Thanks in advance for your help!
References- https://library.linode.com/email/postfix/postfix2.9.6-dovecot2.0.19-mysql#sph_adding-data
- http://stackoverflow.com/questions/24186158/sha512-crypt-mysql-and-dovecot
- http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substring
- http://dev.mysql.com/doc/refman/5.5/en/encryption-functions.html#function_encrypt
- http://dev.mysql.com/doc/refman/5.5/en/encryption-functions.html#function_sha1
- http://stackoverflow.com/questions/24186158/sha512-crypt-mysql-and-dovecot
- http://stackoverflow.com/questions/16485906/mysql-encrypt-password-but-how-to-decrypt-it/16485963#16485963