Fix DBIC create for MySQL in Appendix (Jarom)
[catagits/Catalyst-Manual.git] / lib / Catalyst / Manual / Tutorial / Appendices.pod
CommitLineData
d442cc9f 1=head1 NAME
2
3533daff 3Catalyst::Manual::Tutorial::Appendices - Catalyst Tutorial - Part 10: Appendices
d442cc9f 4
5
6=head1 OVERVIEW
7
3533daff 8This is B<Part 10 of 10> for the Catalyst tutorial.
d442cc9f 9
10L<Tutorial Overview|Catalyst::Manual::Tutorial>
11
12=over 4
13
14=item 1
15
16L<Introduction|Catalyst::Manual::Tutorial::Intro>
17
18=item 2
19
20L<Catalyst Basics|Catalyst::Manual::Tutorial::CatalystBasics>
21
22=item 3
23
3533daff 24L<More Catalyst Basics|Catalyst::Manual::Tutorial::MoreCatalystBasics>
d442cc9f 25
26=item 4
27
3533daff 28L<Basic CRUD|Catalyst::Manual::Tutorial::BasicCRUD>
d442cc9f 29
30=item 5
31
3533daff 32L<Authentication|Catalyst::Manual::Tutorial::Authentication>
d442cc9f 33
34=item 6
35
3533daff 36L<Authorization|Catalyst::Manual::Tutorial::Authorization>
d442cc9f 37
38=item 7
39
3533daff 40L<Debugging|Catalyst::Manual::Tutorial::Debugging>
d442cc9f 41
42=item 8
43
3533daff 44L<Testing|Catalyst::Manual::Tutorial::Testing>
d442cc9f 45
46=item 9
47
3533daff 48L<Advanced CRUD|Catalyst::Manual::Tutorial::AdvancedCRUD>
49
50=item 10
51
d442cc9f 52B<Appendices>
53
54=back
55
56
57=head1 DESCRIPTION
58
59This part of the tutorial provides supporting information relevant to
60the Catalyst tutorial.
61
62
63=head1 APPENDIX 1: CUT AND PASTE FOR POD-BASED EXAMPLES
64
65You may notice that Pod indents example code with four spaces. This
66section provides some quick advice to "un-indent" this text in common
67editors.
68
69=head2 "Un-indenting" with Vi/Vim
70
71When cutting and pasting multi-line text from Pod-based documents, the
72following vi/vim regexs can be helpful to "un-indent" the inserted text
73(do NOT type the quotes, they are only included to show spaces in the
74regex patterns). I<Note that all 3 of the regexs end in 4 spaces>:
75
76=over 4
77
78=item *
79
80":0,$s/^ "
81
82Removes four leading spaces from the entire file (from the first line,
83C<0>, to the last line, C<$>).
84
85=item *
86
87"%s/^ "
88
89A shortcut for the previous item (C<%> specifies the entire file; so
90this removes four leading spaces from every line).
91
92=item *
93
94":.,$s/^ "
95
96Removes the first four spaces from the line the cursor is on at the time
97the regex command is executed (".") to the last line of the file.
98
99=item *
100
101":.,44s/^ "
102
103Removes four leading space from the current line through line 44
104(obviously adjust the C<44> to the appropriate value in your example).
105
106=back
107
108=head2 "Un-indenting" with Emacs
109
191dee29 110Although there author has not used Emacs for many years (apologies to
111the Emacs fans out there), here is a quick hint to get you started. To
d442cc9f 112replace the leading spaces of every line in a file, use:
113
114 M-x replace-regexp<RET>
115 Replace regexp: ^ <RET>
116 with: <RET>
117
118All of that will occur on the single line at the bottom of your screen.
119Note that "<RET>" represents the return key/enter. Also, there are
120four spaces after the "^" on the "Replace regexp:" line and no spaces
121entered on the last line.
122
123You can limit the replacement operation by selecting text first (depending
191dee29 124on your version of Emacs, you can either use the mouse or experiment with
d442cc9f 125commands such as C<C-SPC> to set the mark at the cursor location and
126C<C-E<lt>> and C<C-E<gt>> to set the mark at the beginning and end of the
127file respectively.
128
129
130=head1 APPENDIX 2: USING MYSQL AND POSTGRESQL
131
132The main database used in this tutorial is the very simple yet powerful
133SQLite. This section provides information that can be used to "convert"
134the tutorial to use MySQL and PostgreSQL. However, note that part of
135the beauty of the MVC architecture is that very little database-specific
136code is spread throughout the system (at least when MVC is "done
137right"). Consequently, converting from one database to another is
138relatively painless with most Catalyst applications. In general, you
139just need to adapt the schema definition C<.sql> file you use to
140initialize your database and adjust a few configuration parameters.
141
142Also note that the purpose of the data definition statements for this
143section are not designed to take maximum advantage of the various
144features in each database for issues such as referential integrity and
145field types/constraints.
146
147=head2 MySQL
148
149Use the following steps to adapt the tutorial to MySQL. Thanks to Jim
150Howard for the help.
151
152=over 4
153
154=item *
155
156Part 2: Catalyst Basics
157
158=over 4
159
160=item *
161
162Install the required software:
163
164=over 4
165
166=item *
167
168The MySQL database server and client utility.
169
170=item *
171
172The Perl C<DBD::MySQL> module
173
174=back
175
176For CentOS users (see
177L<Catalyst::Manual::Installation::CentOS4|Catalyst::Manual::Installation::CentOS4>),
178you can use the following commands to install the software and start the MySQL
179daemon:
180
181 yum -y install mysql mysql-server
182 service mysqld start
183
184=item *
185
186Create the database and set the permissions:
187
188 $ mysql
189 Welcome to the MySQL monitor. Commands end with ; or \g.
190 Your MySQL connection id is 2 to server version: 4.1.20
191
192 Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
193
194 mysql> create database myapp;
195 Query OK, 1 row affected (0.01 sec)
196
197 mysql> grant all on myapp.* to tutorial@'localhost';
198 Query OK, 0 rows affected (0.00 sec)
199
200 mysql> flush privileges;
201 Query OK, 0 rows affected (0.00 sec)
202
203 mysql> quit
204 Bye
205
206=item *
207
208Create the C<.sql> file and load the data:
209
210=over 4
211
212=item *
213
214Open the C<myapp01_mysql.sql> in your editor and enter:
215
216 --
217 -- Create a very simple database to hold book and author information
218 --
219 DROP TABLE IF EXISTS books;
220 DROP TABLE IF EXISTS book_authors;
221 DROP TABLE IF EXISTS authors;
222 CREATE TABLE books (
223 id INT(11) PRIMARY KEY AUTO_INCREMENT,
224 title TEXT ,
225 rating INT(11)
226 );
227 -- 'book_authors' is a many-to-many join table between books & authors
228 CREATE TABLE book_authors (
229 book_id INT(11),
230 author_id INT(11),
231 PRIMARY KEY (book_id, author_id)
232 );
233 CREATE TABLE authors (
234 id INT(11) PRIMARY KEY AUTO_INCREMENT,
235 first_name TEXT,
236 last_name TEXT
237 );
238 ---
239 --- Load some sample data
240 ---
241 INSERT INTO books VALUES (1, 'CCSP SNRS Exam Certification Guide', 5);
242 INSERT INTO books VALUES (2, 'TCP/IP Illustrated, Volume 1', 5);
243 INSERT INTO books VALUES (3, 'Internetworking with TCP/IP Vol.1', 4);
244 INSERT INTO books VALUES (4, 'Perl Cookbook', 5);
245 INSERT INTO books VALUES (5, 'Designing with Web Standards', 5);
246 INSERT INTO authors VALUES (1, 'Greg', 'Bastien');
247 INSERT INTO authors VALUES (2, 'Sara', 'Nasseh');
248 INSERT INTO authors VALUES (3, 'Christian', 'Degu');
249 INSERT INTO authors VALUES (4, 'Richard', 'Stevens');
250 INSERT INTO authors VALUES (5, 'Douglas', 'Comer');
251 INSERT INTO authors VALUES (6, 'Tom', 'Christiansen');
252 INSERT INTO authors VALUES (7, ' Nathan', 'Torkington');
253 INSERT INTO authors VALUES (8, 'Jeffrey', 'Zeldman');
254 INSERT INTO book_authors VALUES (1, 1);
255 INSERT INTO book_authors VALUES (1, 2);
256 INSERT INTO book_authors VALUES (1, 3);
257 INSERT INTO book_authors VALUES (2, 4);
258 INSERT INTO book_authors VALUES (3, 5);
259 INSERT INTO book_authors VALUES (4, 6);
260 INSERT INTO book_authors VALUES (4, 7);
261 INSERT INTO book_authors VALUES (5, 8);
262
263=item *
264
265Load the data:
266
267 mysql -ututorial myapp < myapp01_mysql.sql
268
269=item *
270
271Make sure the data loaded correctly:
272
273 $ mysql -ututorial myapp
274 Reading table information for completion of table and column names
275 You can turn off this feature to get a quicker startup with -A
276
277 Welcome to the MySQL monitor. Commands end with ; or \g.
278 Your MySQL connection id is 4 to server version: 4.1.20
279
280 Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
281
282 mysql> show tables;
283 +-----------------+
284 | Tables_in_myapp |
285 +-----------------+
286 | authors |
287 | book_authors |
288 | books |
289 +-----------------+
290 3 rows in set (0.00 sec)
291
292 mysql> select * from books;
293 +----+------------------------------------+--------+
294 | id | title | rating |
295 +----+------------------------------------+--------+
296 | 1 | CCSP SNRS Exam Certification Guide | 5 |
297 | 2 | TCP/IP Illustrated, Volume 1 | 5 |
298 | 3 | Internetworking with TCP/IP Vol.1 | 4 |
299 | 4 | Perl Cookbook | 5 |
300 | 5 | Designing with Web Standards | 5 |
301 +----+------------------------------------+--------+
302 5 rows in set (0.00 sec)
303
304 mysql>
305
306=back
307
308=item *
309
310Update the model:
311
312=over 4
313
314=item *
315
316Delete the existing model:
317
318 rm lib/MyApp/Model/MyAppDB.pm
319
320=item *
321
322Regenerate the model using the Catalyst "_create.pl" script:
323
dfb63992 324 script/myapp_create.pl model MyAppDB DBIC::Schema MyApp::Schema dbi:mysql:myapp 'tutorial' '' '{ AutoCommit => 1 }'
d442cc9f 325
326=back
327
328=back
329
330=item *
331
332Part 4: Authentication
333
334=over 4
335
336=item *
337
338Create the C<.sql> file for the user/roles data:
339
340Open C<myapp02_mysql.sql> in your editor and enter:
341
342 --
343 -- Add users and roles tables, along with a many-to-many join table
344 --
345 CREATE TABLE users (
346 id INT(11) PRIMARY KEY,
347 username TEXT,
348 password TEXT,
349 email_address TEXT,
350 first_name TEXT,
351 last_name TEXT,
352 active INT(11)
353 );
354 CREATE TABLE roles (
355 id INTEGER PRIMARY KEY,
356 role TEXT
357 );
358 CREATE TABLE user_roles (
359 user_id INT(11),
360 role_id INT(11),
361 PRIMARY KEY (user_id, role_id)
362 );
363 --
364 -- Load up some initial test data
365 --
366 INSERT INTO users VALUES (1, 'test01', 'mypass', 't01@na.com', 'Joe', 'Blow', 1);
367 INSERT INTO users VALUES (2, 'test02', 'mypass', 't02@na.com', 'Jane', 'Doe', 1);
368 INSERT INTO users VALUES (3, 'test03', 'mypass', 't03@na.com', 'No', 'Go', 0);
369 INSERT INTO roles VALUES (1, 'user');
370 INSERT INTO roles VALUES (2, 'admin');
371 INSERT INTO user_roles VALUES (1, 1);
372 INSERT INTO user_roles VALUES (1, 2);
373 INSERT INTO user_roles VALUES (2, 1);
374 INSERT INTO user_roles VALUES (3, 1);
375
376=item *
377
378Load the user/roles data:
379
380 mysql -ututorial myapp < myapp02_mysql.sql
381
382=item *
383
384Create the C<.sql> file for the hashed password data:
385
386Open C<myapp03_mysql.sql> in your editor and enter:
387
388 --
389 -- Convert passwords to SHA-1 hashes
390 --
391 UPDATE users SET password = 'e727d1464ae12436e899a726da5b2f11d8381b26' WHERE id = 1;
392 UPDATE users SET password = 'e727d1464ae12436e899a726da5b2f11d8381b26' WHERE id = 2;
393 UPDATE users SET password = 'e727d1464ae12436e899a726da5b2f11d8381b26' WHERE id = 3;
394
395=item *
396
397Load the user/roles data:
398
399 mysql -ututorial myapp < myapp03_mysql.sql
400
401=back
402
403=back
404
405=head2 PostgreSQL
406
3533daff 407Use the following steps to adapt the tutorial to PostgreSQL. Thanks to
408Louis Moore for the help who was in turn helped by Marcello Romani and
409Tom Lanyon.
410
411=over 4
412
413=item *
414
415Part 2: Catalyst Basics
416
417=over 4
418
419=item *
420
421Install the required software:
422
423=over 4
424
425=item *
426
427The PostgreSQL database server and client.
428
429=item *
430
431The Perl C<DBD::Pg> module
432
433=back
434
435=item *
436
437Create the database and a user for the database
438
439 $ createuser -P catmyapp
440 Enter password for new role: <catalyst>
441 Enter it again: <catalyst>
442 Shall the new role be a superuser? (y/n) n
443 Shall the new role be allowed to create databases? (y/n) n
444 Shall the new role be allowed to create more new roles? (y/n) n
445 CREATE ROLE
446 $ createdb -O catmyapp mycatapp
447 CREATE DATABASE
448
449=item *
450
451Create the C<.sql> file and load the data:
452
453=over 4
454
455=item *
456
457Open the C<myapp01_psql.sql> in your editor and enter:
458
459
1390ef0e 460 --
461 -- Create a very simple database to hold book and author information
462 --
463 -- The sequence is how we get a unique id in PostgreSQL
464 --
465 CREATE SEQUENCE books_seq START 5 ;
466 SELECT nextval ('books_seq');
467
468 CREATE TABLE books (
469 id INTEGER PRIMARY KEY DEFAULT nextval('books_seq'),
470 title TEXT ,
471 rating INTEGER
472 );
473
474 -- 'book_authors' is a many-to-many join table between books & authors
475 CREATE TABLE book_authors (
476 book_id INTEGER,
477 author_id INTEGER,
478 PRIMARY KEY (book_id, author_id)
479 );
480
481 CREATE SEQUENCE authors_seq START 8 ;
482 SELECT nextval ('authors_seq');
483
484 CREATE TABLE authors (
485 id INTEGER PRIMARY KEY DEFAULT nextval('authors_seq'),
486 first_name TEXT,
487 last_name TEXT
488 );
489 ---
490 --- Load some sample data
491 ---
492 INSERT INTO books VALUES (1, 'CCSP SNRS Exam Certification Guide', 5);
493 INSERT INTO books VALUES (2, 'TCP/IP Illustrated, Volume 1', 5);
494 INSERT INTO books VALUES (3, 'Internetworking with TCP/IP Vol.1', 4);
495 INSERT INTO books VALUES (4, 'Perl Cookbook', 5);
496 INSERT INTO books VALUES (5, 'Designing with Web Standards', 5);
497 INSERT INTO authors VALUES (1, 'Greg', 'Bastien');
498 INSERT INTO authors VALUES (2, 'Sara', 'Nasseh');
499 INSERT INTO authors VALUES (3, 'Christian', 'Degu');
500 INSERT INTO authors VALUES (4, 'Richard', 'Stevens');
501 INSERT INTO authors VALUES (5, 'Douglas', 'Comer');
502 INSERT INTO authors VALUES (6, 'Tom', 'Christiansen');
503 INSERT INTO authors VALUES (7, 'Nathan', 'Torkington');
504 INSERT INTO authors VALUES (8, 'Jeffrey', 'Zeldman');
505 INSERT INTO book_authors VALUES (1, 1);
506 INSERT INTO book_authors VALUES (1, 2);
507 INSERT INTO book_authors VALUES (1, 3);
508 INSERT INTO book_authors VALUES (2, 4);
509 INSERT INTO book_authors VALUES (3, 5);
510 INSERT INTO book_authors VALUES (4, 6);
511 INSERT INTO book_authors VALUES (4, 7);
512 INSERT INTO book_authors VALUES (5, 8);
3533daff 513
514=item *
515
516Load the data:
517
518 $ psql -U catmyapp -W mycatapp
519 Password for user catmyapp: catalyst
520 Welcome to psql 8.1.8, the PostgreSQL interactive terminal.
1390ef0e 521
3533daff 522 Type: \copyright for distribution terms
523 \h for help with SQL commands
524 \? for help with psql commands
525 \g or terminate with semicolon to execute query
526 \q to quit
1390ef0e 527
3533daff 528 mycatapp=> \i myapp01_psql.sql
1390ef0e 529
3533daff 530 CREATE SEQUENCE
531 nextval
532 ---------
533 5
534 (1 row)
1390ef0e 535
3533daff 536 psql:myapp01_psql.sql:11: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "books_pkey" for table "books"
537 CREATE TABLE
538 psql:myapp01_psql.sql:19: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "book_authors_pkey" for table
539 "book_authors"
540 CREATE TABLE
541 CREATE SEQUENCE
542 nextval
543 ---------
544 8
545 (1 row)
1390ef0e 546
3533daff 547 psql:myapp01_psql.sql:30: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "authors_pkey" for table "authors"
548 CREATE TABLE
549 INSERT 0 1
550 INSERT 0 1
551 INSERT 0 1
552 INSERT 0 1
553 ...
554
555=item *
556
557Make sure the data loaded correctly:
558
559 mycatapp=> \dt
560 List of relations
561 Schema | Name | Type | Owner
562 --------+--------------+-------+----------
563 public | authors | table | catmyapp
564 public | book_authors | table | catmyapp
565 public | books | table | catmyapp
566 (3 rows)
1390ef0e 567
3533daff 568 mycatapp=> select * from books;
569 id | title | rating
570 ----+------------------------------------+--------
571 1 | CCSP SNRS Exam Certification Guide | 5
572 2 | TCP/IP Illustrated, Volume 1 | 5
573 3 | Internetworking with TCP/IP Vol.1 | 4
574 4 | Perl Cookbook | 5
575 5 | Designing with Web Standards | 5
576 (5 rows)
1390ef0e 577
3533daff 578 mycatapp=> \q
579
580=back
581
582=item *
583
584After the steps where you:
585
586 edit lib/MyApp.pm
1390ef0e 587
3533daff 588 create lib/MyAppDB.pm
1390ef0e 589
3533daff 590 create lib/MyAppDB/Book.pm
1390ef0e 591
3533daff 592 create lib/MyAppDB/Author.pm
1390ef0e 593
3533daff 594 create lib/MyAppDB/BookAuthor.pm
595
596
597=item *
598
599Generate the model using the Catalyst "_create.pl" script:
600
601 script/myapp_create.pl model MyAppDB DBIC::Schema MyAppDB 'dbi:Pg:dbname=mycatapp' 'catmyapp' 'catalyst' '{ AutoCommit => 1 }'
602
603
604=back
605
606=item *
607
608Part 4: Authentication
609
610=over 4
611
612=item *
613
614Create the C<.sql> file for the user/roles data:
615
616Open C<myapp02_psql.sql> in your editor and enter:
617
618 --
619 -- Add users and roles tables, along with a many-to-many join table
620 --
1390ef0e 621
3533daff 622 CREATE SEQUENCE users_seq START 3 ;
623 SELECT nextval ('users_seq');
1390ef0e 624
3533daff 625 CREATE TABLE users (
626 id INTEGER PRIMARY KEY DEFAULT nextval('users_seq'),
627 username TEXT,
628 password TEXT,
629 email_address TEXT,
630 first_name TEXT,
631 last_name TEXT,
632 active INTEGER
633 );
1390ef0e 634
3533daff 635 CREATE SEQUENCE roles_seq START 2 ;
636 SELECT nextval ('roles_seq');
1390ef0e 637
3533daff 638 CREATE TABLE roles (
639 id INTEGER PRIMARY KEY DEFAULT nextval('roles_seq'),
640 role TEXT
641 );
1390ef0e 642
3533daff 643 CREATE TABLE user_roles (
644 user_id INTEGER,
645 role_id INTEGER,
646 PRIMARY KEY (user_id, role_id)
647 );
1390ef0e 648
3533daff 649 --
650 -- Load up some initial test data
651 --
652 INSERT INTO users VALUES (1, 'test01', 'mypass', 't01@na.com', 'Joe', 'Blow', 1);
653 INSERT INTO users VALUES (2, 'test02', 'mypass', 't02@na.com', 'Jane', 'Doe', 1);
654 INSERT INTO users VALUES (3, 'test03', 'mypass', 't03@na.com', 'No', 'Go', 0);
655 INSERT INTO roles VALUES (1, 'user');
656 INSERT INTO roles VALUES (2, 'admin');
657 INSERT INTO user_roles VALUES (1, 1);
658 INSERT INTO user_roles VALUES (1, 2);
659 INSERT INTO user_roles VALUES (2, 1);
660 INSERT INTO user_roles VALUES (3, 1);
661
662=item *
663
664Load the data:
665
666 $ psql -U catmyapp -W mycatapp
667 Password for user catmyapp: catalyst
668 Welcome to psql 8.1.8, the PostgreSQL interactive terminal.
1390ef0e 669
3533daff 670 Type: \copyright for distribution terms
671 \h for help with SQL commands
672 \? for help with psql commands
673 \g or terminate with semicolon to execute query
674 \q to quit
1390ef0e 675
3533daff 676 mycatapp=> \i myapp02_psql.sql
1390ef0e 677
3533daff 678 CREATE SEQUENCE
679 nextval
680 ---------
681 3
682 (1 row)
1390ef0e 683
3533daff 684 psql:myapp02_psql.sql:16: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "users_pkey" for table "users"
685 CREATE TABLE
686 CREATE SEQUENCE
687 nextval
688 ---------
689 2
690 (1 row)
1390ef0e 691
3533daff 692 psql:myapp02_psql.sql:24: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "roles_pkey" for table "roles"
693 CREATE TABLE
694 psql:myapp02_psql.sql:30: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "user_roles_pkey" for table "user_roles"
695 CREATE TABLE
696 INSERT 0 1
697 INSERT 0 1
698 INSERT 0 1
699 INSERT 0 1
700 INSERT 0 1
701 INSERT 0 1
702 INSERT 0 1
703 INSERT 0 1
704 INSERT 0 1
705 mycatapp=>
1390ef0e 706
3533daff 707 mycatapp=> select * from users;
708 id | username | password | email_address | first_name | last_name | active
709 ----+----------+----------+---------------+------------+-----------+--------
710 1 | test01 | mypass | t01@na.com | Joe | Blow | 1
711 2 | test02 | mypass | t02@na.com | Jane | Doe | 1
712 3 | test03 | mypass | t03@na.com | No | Go | 0
713 (3 rows)
714
715
716=item *
717
718Create the C<.sql> file for the hashed password data:
719
720Open C<myapp03_psql.sql> in your editor and enter:
721
722 --
723 -- Convert passwords to SHA-1 hashes
724 --
725 UPDATE users SET password = 'e727d1464ae12436e899a726da5b2f11d8381b26' WHERE id = 1;
726 UPDATE users SET password = 'e727d1464ae12436e899a726da5b2f11d8381b26' WHERE id = 2;
727 UPDATE users SET password = 'e727d1464ae12436e899a726da5b2f11d8381b26' WHERE id = 3;
728
729=item *
730
731Load in the data
732
733 $ psql -U catmyapp -W mycatapp
734 Password for user catmyapp:
735 Welcome to psql 8.1.8, the PostgreSQL interactive terminal.
1390ef0e 736
3533daff 737 Type: \copyright for distribution terms
738 \h for help with SQL commands
739 \? for help with psql commands
740 \g or terminate with semicolon to execute query
741 \q to quit
1390ef0e 742
3533daff 743 mycatapp=> \i myapp03_psql.sql
744 UPDATE 1
745 UPDATE 1
746 UPDATE 1
747
748
749
750=back
751
752=back
d442cc9f 753
754
755=head1 APPENDIX 3: IMPROVED HASHING SCRIPT
756
757Here is an improved SHA-1 hashing script from Gavin Henry that does
758not expose the passwords to "capture" on the command line.
759
760 #!/usr/bin/perl -w
761 #===============================================================================
762 #
763 # FILE: enc_pass.pl
764 #
765 # USAGE: ./enc_pass.pl
766 #
767 # DESCRIPTION: Encrypt a Password using SHA-1
768 #
769 # OPTIONS: ---
770 # REQUIREMENTS: ---
771 # BUGS: ---
772 # NOTES: ---
773 # AUTHOR: Gavin Henry (GH), <ghenry@suretecsystems.com>
774 # COMPANY: Suretec Systems Ltd.
775 # VERSION: 1.0
776 # CREATED: 26/06/2006
777 # REVISION: ---
778 # COPYRIGHT: http://search.cpan.org/dist/perl/pod/perlgpl.pod
779 #===============================================================================
780
781 use strict;
782 use warnings;
783 use Digest::SHA1;
784 use Term::ReadKey;
785
786 sub get_pass {
787 ReadMode 'noecho';
788 chomp( my $pw = ReadLine 0 );
789 ReadMode 'normal';
790 return $pw;
791 }
792
793 print "Enter the password to be encrypted: ";
794 my $pass = get_pass();
795
796 print "\nConfirm the password: ";
797 my $verify = get_pass();
798
799 if ( $pass eq $verify ) {
800 my $sha1_enc = Digest::SHA1->new;
801 $sha1_enc->add($pass);
802
803 print "\nYour encrypted password is: "
804 . $sha1_enc->hexdigest . "\n"
805 . "Paste this into your SQL INSERT/COPY Data.\n";
806 }
807 else {
808 print "\nPasswords do not match!\n";
809 }
810
811
812=head1 AUTHOR
813
814Kennedy Clark, C<hkclark@gmail.com>
815
816Please report any errors, issues or suggestions to the author. The
817most recent version of the Catalyst Tutorial can be found at
82ab4bbf 818L<http://dev.catalyst.perl.org/repos/Catalyst/Catalyst-Manual/5.70/trunk/lib/Catalyst/Manual/Tutorial/>.
d442cc9f 819
45c7830f 820Copyright 2006-2008, Kennedy Clark, under Creative Commons License
95674086 821(L<http://creativecommons.org/licenses/by-sa/3.0/us/>).