title: sha512-crypt mysql and dovecot
body: <p>I have a question about understanding sha512-crypt hashing. I found this <a href="https://library.linode.com/email/postfix/postfix2.9.6-dovecot2.0.19-mysql" rel="noreferrer">tutorial</a> to set up dovecot and postfix with mysql. I followed the tutorial (with slight modifications) and everything works fine. But there is one thing, that I do not understand:</p>
  
  <p>To add a user, I should use:</p>
  
  <pre><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');
  </code></pre>
  
  <p>and again, this works perfectly fine, i.e. I can log in with my password (and only my password) to dovecot. But why? If I see it right, it encrypts the password with a random salt, but it doesn't save it anywhere. So hashing the same password twice gives me 2 different hashes (I tried it). So my question boils down to:
  Could I get a brief explanation of sha-512 (which I couldn't find online) and and explanation as to why these lines work?</p>
  
  <p>Thanks already </p>