Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » General » Downloads
 
Add your own
alternative version
Go to top

Password Safe Database Reader Library in C# for .NET

, 16 Oct 2007
An independent library implementation to read Password Safe Password Manager V3 database files
#region License
/*
 *  PasswordSafe Database Reader/Writer
 *
 *  Copyright (C) 2007 Svante Seleborg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *  If you'd like to license this program under any other terms than the
 *  above, please contact the author and copyright holder.
 *
 *  Contact: mailto:svante@axantum.com
 */
#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;

using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;

namespace Axantum.PasswordSafe.Test
{
    [TestFixture]
    public class TestPasswordSafeReader
    {
        [NUnit.Framework.Test]
        public void TestIt()
        {
            Stream pwsafeStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Axantum.PasswordSafe.Test.Resources.pwsafe.psafe3");
            using (PasswordSafeReader reader = new PasswordSafeReader(pwsafeStream))
            {
                reader.SetPassphrase("R�ksm�rg�s99");
                while (reader.Read())
                {
                    switch (reader.CurrentPartType)
                    {
                        case PasswordSafePartType.Header:
                            PasswordSafeHeader header = reader.Header;
                            Assert.That(header.DBDescription, Is.EqualTo(null), "DbDescription should be null");
                            Assert.That(header.DBName, Is.EqualTo(null), "DbName should be null");
                            Assert.That(header.LastSavedBy, Is.EqualTo("Svante"));
                            Assert.That(header.LastSavedOn, Is.EqualTo("CORUSCANT"));
                            Assert.That(header.LastUpdateHost, Is.EqualTo(null));
                            Assert.That(header.LastUpdateUser, Is.EqualTo(null));
                            Assert.That(header.MajorVersion, Is.EqualTo(3));
                            Assert.That(header.MinorVersion, Is.EqualTo(1));
                            Assert.That(header.TreeDisplayStatus.Count, Is.EqualTo(1));
                            Assert.That(header.TreeDisplayStatus[0], Is.EqualTo(false));
                            Assert.That(header.Uuid, Is.EqualTo(new Guid("{8a0e8e31-9885-d142-9c67-3fa78e63de61}")));
                            Assert.That(header.WhatLastSaved, Is.EqualTo("Password Safe V3.08"));
                            Assert.That(header.NonDefaultUserPrefs, Is.EqualTo(String.Empty));
                            break;

                        case PasswordSafePartType.Record:
                            PasswordSafeRecord record = reader.Record;
                            Assert.That(record.AutoType, Is.EqualTo("Autotype1"), "AutoType");
                            Assert.That(record.Notes, Is.EqualTo("Note1"), "Notes");
                            Assert.That(record.PasswordHistory.Status, Is.EqualTo(1), "PasswordHistory.Status");
                            Assert.That(record.PasswordHistory.MaxHistory, Is.EqualTo(3), "PasswordHistory.MaxHistory");
                            Assert.That(record.PasswordHistory.PasswordHistory.Count, Is.EqualTo(2), "PasswordHistory.Count");
                            Assert.That(record.Password.Password, Is.EqualTo("Password3"), "Password");
                            Assert.That(record.Password.ExpiresUtc, Is.EqualTo(new DateTime(2010, 12, 31, 11, 34, 0, DateTimeKind.Utc)), "ExpiresUtc");
                            Assert.That(record.Password.ModifiedUtc, Is.EqualTo(new DateTime(2007, 10, 11, 09, 51, 29, DateTimeKind.Utc)), "ModifiedUtc");
                            Assert.That(record.TimeRecordAccessedUtc, Is.EqualTo(DateTime.MinValue), "TimeRecordAccessedUtc");
                            Assert.That(record.TimeRecordCreatedUtc, Is.EqualTo(new DateTime(2007, 10, 11, 09, 50, 30, DateTimeKind.Utc)), "TimeRecordCreatedUtc");
                            Assert.That(record.TimeRecordModifiedUtc, Is.EqualTo(new DateTime(2007, 10, 11, 09, 51, 29, DateTimeKind.Utc)), "TimeRecordModifiedUtc");
                            Assert.That(record.Title, Is.EqualTo("Title1"));
                            Assert.That(record.ResourceLocator, Is.EqualTo("http://www.url1.com/"));
                            Assert.That(record.User, Is.EqualTo("Username1"));
                            Assert.That(record.Uuid, Is.EqualTo(new Guid("{3973f18e-c986-034f-a974-acaa2d4c31cb}")));
                            break;

                        case PasswordSafePartType.End:
                            break;

                        default:
                            Assert.Fail("Unexpected part type");
                            break;
                    }
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

Svante Seleborg
Web Developer Axantum Software AB
Sweden Sweden
I've been working with all aspects of software development since 1979 - from compiler construction to management. Currently I'm an independent consultant mostly specializing in computer security. Please see my homepage for contact details.
 
I speak C like a native, and have a pretty good grasp of C++. The most recent five years C# has been the main development language. Traditionally Unix has been the dominating environment, but currently the scales have tipped over to Windows, due to market demands but I'm equally at home developing in both environments.
 
When I'm not coding I'm usually sitting on one of my 4 bikes, indoors or outdoors, on the road or in the woods.

| Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 16 Oct 2007
Article Copyright 2007 by Svante Seleborg
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid