# Text manipulation with awk (generating password hashes)



## dennylin93 (Aug 24, 2009)

Hi, I'm creating virtual users with MySQL, and I don't know how to generate the hashed versions of the passwords.

input:

```
username1       password1
username2       password2
username3       password3
```

convert.sh:

```
#!/bin/sh

INPUT=./input
OUTPUT=./output
AWK=/usr/bin/awk

$AWK '{printf("INSERT INTO `virtual_users` VALUES (NULL, '\''%s'\'', '\''%s'\'');\n", $1, $2)}' $INPUT > $OUTPUT
```

I need to use `$ dovecotpw -s SSHA256 -p password` to insert the hashed string into the output, but I don't know if it can be done with awk. Can anyone point me in the right direction? Thanks.


----------



## SirDice (Aug 24, 2009)

Use the MySQL password function.

http://dev.mysql.com/doc/refman/5.1/en/password-hashing.html

Some like:

```
INSERT INTO 'virtual_users' VALUES (NULL, '%s', PASSWORD('%S'));
```


----------



## vermaden (Aug 24, 2009)

@dennylin93

Try like that:
`% awk 'BEGIN { system("dovecotpw -s SSHA256 -p password"); }'`


----------



## Alt (Aug 24, 2009)

Seems these awks completely unusable, never liked them :e
So, we can do


> cat $INPUT | perl -ne '/^(.*?)[\s\t]+(.*?)$/; printf("insert into test (a, b) values ('%s', '%s')\n", $1, qx/dovecotpw -s SSHA256 -p $2/);'


Or


> cat tttt | perl -ne 'split; printf("insert into test (a, b) values (\"%s\", \"%s\")\n", $_[0], qx/dovecotpw -s SSHA256 -p $_[1]/);'


----------



## dennylin93 (Aug 24, 2009)

@SirDice: I forgot that MySQL has password functions as well. I'm not sure if they're compatible with Dovecot though, so I'll test them later.

@vermaden: Not really sure how to use awk properly (I'm a beginner right now). I'll stick to Perl for the moment.

@Alt: The command works perfectly.

Thanks again for all the help.


----------

