Skip to content

Instantly share code, notes, and snippets.

@lettergram
Created March 18, 2015 23:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lettergram/079432c7f42a77771b9c to your computer and use it in GitHub Desktop.
Save lettergram/079432c7f42a77771b9c to your computer and use it in GitHub Desktop.
// Generates a salt for the given user
func GenerateSalt(user string, pass string) string {
var password []byte
var username []byte
username = []byte("user:" + user + pass)
// Dial up a mongoDB session
session, _ := mgo.Dial("127.0.0.1:27017/")
// Opens the "passwords" databases, "salts" collection
c := session.DB("passwords").C("salts")
// Result with store username + password
result := Salting_Struct{}
// Generate random number, then use SHA-2 algorithm for salt
// Take only the first 12 digits for salt
result.Salt := SHA(string(rand.Intn(10000000)))[12:]
// Search for the salted username, place in result the salt
c.Insert(&result)
// close mongoDB session to free resources
session.Close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment