pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/passwd/example_test.go (about) 1 package passwd 2 3 // ////////////////////////////////////////////////////////////////////////////////// // 4 // // 5 // Copyright (c) 2022 ESSENTIAL KAOS // 6 // Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // 7 // // 8 // ////////////////////////////////////////////////////////////////////////////////// // 9 10 import ( 11 "fmt" 12 ) 13 14 // ////////////////////////////////////////////////////////////////////////////////// // 15 16 func ExampleHash() { 17 password, pepper := "MyPassword", "ABCD1234abcd1234" 18 hash, err := Hash(password, pepper) 19 20 if err != nil { 21 panic(err.Error()) 22 } 23 24 fmt.Printf("Hash: %s\n", hash) 25 } 26 27 func ExampleHashBytes() { 28 password, pepper := []byte("MyPassword"), []byte("ABCD1234abcd1234") 29 hash, err := HashBytes(password, pepper) 30 31 if err != nil { 32 panic(err.Error()) 33 } 34 35 fmt.Printf("Hash: %s\n", hash) 36 } 37 38 func ExampleCheck() { 39 password, pepper := "MyPassword", "ABCD1234abcd1234" 40 hash, err := Hash(password, pepper) 41 42 if err != nil { 43 panic(err.Error()) 44 } 45 46 fmt.Printf("Valid: %t\n", Check(password, pepper, hash)) 47 48 // Output: 49 // Valid: true 50 } 51 52 func ExampleCheckBytes() { 53 password, pepper := []byte("MyPassword"), []byte("ABCD1234abcd1234") 54 hash, err := HashBytes(password, pepper) 55 56 if err != nil { 57 panic(err.Error()) 58 } 59 60 fmt.Printf("Valid: %t\n", CheckBytes(password, pepper, hash)) 61 62 // Output: 63 // Valid: true 64 } 65 66 func ExampleGenPassword() { 67 weakPassword := GenPassword(16, STRENGTH_WEAK) 68 medPassword := GenPassword(16, STRENGTH_MEDIUM) 69 strongPassword := GenPassword(16, STRENGTH_STRONG) 70 71 fmt.Printf("Weak password: %s\n", weakPassword) 72 fmt.Printf("Medium password: %s\n", medPassword) 73 fmt.Printf("Strong password: %s\n", strongPassword) 74 } 75 76 func ExampleGenPasswordBytes() { 77 weakPassword := GenPasswordBytes(16, STRENGTH_WEAK) 78 medPassword := GenPasswordBytes(16, STRENGTH_MEDIUM) 79 strongPassword := GenPasswordBytes(16, STRENGTH_STRONG) 80 81 fmt.Printf("Weak password: %s\n", weakPassword) 82 fmt.Printf("Medium password: %s\n", medPassword) 83 fmt.Printf("Strong password: %s\n", strongPassword) 84 } 85 86 func ExampleGetPasswordStrength() { 87 strength := GetPasswordStrength("secret1234%$") 88 89 switch strength { 90 case STRENGTH_STRONG: 91 fmt.Println("Password is strong") 92 case STRENGTH_MEDIUM: 93 fmt.Println("Password is ok") 94 case STRENGTH_WEAK: 95 fmt.Println("Password is weak") 96 } 97 98 // Output: 99 // Password is ok 100 } 101 102 func ExampleGetPasswordBytesStrength() { 103 strength := GetPasswordBytesStrength([]byte("secret1234%$")) 104 105 switch strength { 106 case STRENGTH_STRONG: 107 fmt.Println("Password is strong") 108 case STRENGTH_MEDIUM: 109 fmt.Println("Password is ok") 110 case STRENGTH_WEAK: 111 fmt.Println("Password is weak") 112 } 113 114 // Output: 115 // Password is ok 116 } 117 118 func ExampleGenPasswordVariations() { 119 password := "myPassword12345" 120 variants := GenPasswordVariations(password) 121 122 fmt.Printf("Variants: %v\n", variants) 123 124 // Output: 125 // Variants: [MYpASSWORD12345 MyPassword12345 myPassword1234] 126 } 127 128 func ExampleGenPasswordBytesVariations() { 129 password := []byte("myPassword12345") 130 variants := GenPasswordBytesVariations(password) 131 132 fmt.Printf("Variants: %v\n", variants) 133 134 // Output: 135 // Variants: [[77 89 112 65 83 83 87 79 82 68 49 50 51 52 53] [77 121 80 97 115 115 119 111 114 100 49 50 51 52 53] [109 121 80 97 115 115 119 111 114 100 49 50 51 52]] 136 }