github.com/zntrio/harp/v2@v2.0.9/pkg/sdk/security/diceware/generator.go (about) 1 // Licensed to Elasticsearch B.V. under one or more contributor 2 // license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright 4 // ownership. Elasticsearch B.V. licenses this file to you under 5 // the Apache License, Version 2.0 (the "License"); you may 6 // not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 package diceware 19 20 import ( 21 "fmt" 22 "strings" 23 24 "github.com/sethvargo/go-diceware/diceware" 25 ) 26 27 const ( 28 // MinWordCount defines the lowest bound for allowed word count. 29 MinWordCount = 4 30 // MaxWordCount defines the highest bound for allowed word count. 31 MaxWordCount = 24 32 // BasicWordCount defines basic passphrase word count (4 words). 33 BasicWordCount = 4 34 // StrongWordCount defines strong passphrase word count (8 words). 35 StrongWordCount = 8 36 // ParanoidWordCount defines paranoid passphrase word count (12 words). 37 ParanoidWordCount = 12 38 // MasterWordCount defines master passphrase word count (24 words). 39 MasterWordCount = 24 40 ) 41 42 // Diceware generates a passphrase using english words. 43 func Diceware(count int) (string, error) { 44 // Check parameters 45 if count < MinWordCount { 46 count = MinWordCount 47 } 48 if count > MaxWordCount { 49 count = MaxWordCount 50 } 51 52 // Generate word list 53 list, err := diceware.Generate(count) 54 if err != nil { 55 return "", fmt.Errorf("unable to generate daceware passphrase: %w", err) 56 } 57 58 // Assemble result 59 return strings.Join(list, "-"), nil 60 } 61 62 // Basic generates 4 words diceware passphrase. 63 func Basic() (string, error) { 64 return Diceware(BasicWordCount) 65 } 66 67 // Strong generates 8 words diceware passphrase. 68 func Strong() (string, error) { 69 return Diceware(StrongWordCount) 70 } 71 72 // Paranoid generates 12 words diceware passphrase. 73 func Paranoid() (string, error) { 74 return Diceware(ParanoidWordCount) 75 } 76 77 // Master generates 24 words diceware passphrase. 78 func Master() (string, error) { 79 return Diceware(MasterWordCount) 80 }