Skip to content

Instantly share code, notes, and snippets.

@osiloke
Last active August 21, 2022 08:06
Show Gist options
  • Save osiloke/186a0feb6e203afa01c11d43439a383f to your computer and use it in GitHub Desktop.
Save osiloke/186a0feb6e203afa01c11d43439a383f to your computer and use it in GitHub Desktop.

Revisions

  1. osiloke revised this gist Sep 4, 2017. No changes.
  2. osiloke created this gist Sep 4, 2017.
    45 changes: 45 additions & 0 deletions doveadmpw.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    package main

    import (
    "crypto/sha512"
    "encoding/base64"
    "fmt"
    "math/rand"
    "time"
    )

    const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    const (
    letterIdxBits = 6 // 6 bits to represent a letter index
    letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
    letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
    )

    var src = rand.NewSource(time.Now().UnixNano())

    //http://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-golang
    func RandStringBytesMaskImprSrc(n int) string {
    b := make([]byte, n)
    // A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
    for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
    if remain == 0 {
    cache, remain = src.Int63(), letterIdxMax
    }
    if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
    b[i] = letterBytes[idx]
    i--
    }
    cache >>= letterIdxBits
    remain--
    }

    return string(b)
    }
    func main() {
    salt := RandStringBytesMaskImprSrc(5)
    password := "randompass"
    hasher := sha512.New()
    hasher.Write([]byte(password))
    hasher.Write([]byte(salt))
    fmt.Printf("{SSHA512}%s\n", base64.StdEncoding.EncodeToString([]byte(string(hasher.Sum(nil))+salt)))
    }