passwd

package module
v0.0.0-...-a4128cd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 17, 2016 License: MIT Imports: 8 Imported by: 1

Documentation

Overview

passwd file library for web applications Copyright (c) 2016 aerth import github.com/aerth/passwd MIT License

Example
package main

import (
	"fmt"
	"log"
	"math/rand"
	"strconv"
	"time"

	"github.com/aerth/passwd"
)

var a []int
var c chan int
var cb chan bool
var c1, c2, c3, c4 chan int
var i1, i2 int
var b bool
var err error
var r *rand.Rand

func init() {
	r = rand.New(rand.NewSource(time.Now().UnixNano()))
}

func dummy() {
	user := strconv.FormatUint(uint64(r.Uint32()), 10)
	user2 := strconv.FormatUint(uint64(r.Uint32()), 10)
	err = passwd.Insert(user+user2, []byte("password"))
	if err != nil {
		fmt.Println("Dummy Insert Error: ", err)
		log.Println("Created Dummy:", user+user2)
	} else {
		e := passwd.Write()
		if e != nil {
			fmt.Println("Didn't create dummy:", e)
		} else {
			log.Println("Created Dummy:", user+user2)
		}
	}

}
func main() {

	passwd.Parse()

	dummy()
	dummy()
	dummy()

	b = passwd.Match("root", []byte("pasksword"))
	fmt.Println("Match:", b)
	b = passwd.Match("root", []byte("password"))
	fmt.Println("Match:", b)

	err = passwd.Update("root", []byte("pasksword"))
	if err != nil {
		fmt.Println("Insert Error: ", err)
	} else {
		passwd.Write()
	}
	b = passwd.Match("root", []byte("pasksword"))
	fmt.Println("Match:", b)
	b = passwd.Match("root", []byte("password"))
	fmt.Println("Match:", b)

	// err = passwd.Delete("root")
	// if err != nil {
	// 	fmt.Println("Delete Error: ", err)
	// } else {
	// 	passwd.Write()
	// }
	// b = passwd.Match("root", []byte("pasksword"))
	// fmt.Println("Match:", b)
	// b = passwd.Match("root", []byte("password"))
	// fmt.Println("Match:", b)

	for {
	}

}

func sleep(n int64) {
	time.Sleep(time.Second * 1)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete

func Delete(user string) error

Delete removes a user from the usertable.

func Insert

func Insert(user string, password []byte) error

Insert returns an error if the user exists already. Otherwise, it inserts the user and hashed password into the usertable. Write() must be called to make actual changes to the passwd file.

func InsertOrUpdate

func InsertOrUpdate(user string, password []byte)

InsertOrUpdate returns no error.

func List

func List() []string

List returns a []string list of user identifiers

func Match

func Match(user string, password []byte) bool

Match, for login sequences. Must be called after Parse(). Read-only.

func Parse

func Parse()

Parse stores the usertable to memory to enable Match()

func SetLocation

func SetLocation(loc string)

func Update

func Update(user string, password []byte) error

Update updates a user password.

func UpdateID

func UpdateID(old, new string, p []byte) error

Update updates a id/username from old to new, and if p != nil updates the password.

func Write

func Write() error

Write actually writes the user names to the passwd file after truncating to 0 Before calling Write(), the usertable is only in memory. Starting your code with a "defer passwd.Write()" will hopefully store any changes in the event of a panic Only call once at a time.

Types

type PasswdFile

type PasswdFile struct {
	// contains filtered or unexported fields
}

func New

func New(loc string, key ...[]byte) *PasswdFile

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL