MCPcopy Create free account
hub / github.com/supabase/auth / ParseFirebaseScryptHash

Function ParseFirebaseScryptHash

internal/crypto/password.go:87–158  ·  view source on GitHub ↗

See: https://github.com/firebase/scrypt for implementation

(hash string)

Source from the content-addressed store, hash-verified

85
86// See: https://github.com/firebase/scrypt for implementation
87func ParseFirebaseScryptHash(hash string) (*FirebaseScryptHashInput, error) {
88 submatch := fbscryptHashRegexp.FindStringSubmatchIndex(hash)
89 if submatch == nil {
90 return nil, errors.New("crypto: incorrect scrypt hash format")
91 }
92
93 v := string(fbscryptHashRegexp.ExpandString(nil, "$v", hash, submatch))
94 n := string(fbscryptHashRegexp.ExpandString(nil, "$n", hash, submatch))
95 r := string(fbscryptHashRegexp.ExpandString(nil, "$r", hash, submatch))
96 p := string(fbscryptHashRegexp.ExpandString(nil, "$p", hash, submatch))
97 ss := string(fbscryptHashRegexp.ExpandString(nil, "$ss", hash, submatch))
98 sk := string(fbscryptHashRegexp.ExpandString(nil, "$sk", hash, submatch))
99 saltB64 := string(fbscryptHashRegexp.ExpandString(nil, "$salt", hash, submatch))
100 hashB64 := string(fbscryptHashRegexp.ExpandString(nil, "$hash", hash, submatch))
101
102 if v != "1" {
103 return nil, fmt.Errorf("crypto: Firebase scrypt hash uses unsupported version %q only version 1 is supported", v)
104 }
105 memoryPower, err := strconv.ParseUint(n, 10, 32)
106 if err != nil {
107 return nil, fmt.Errorf("crypto: Firebase scrypt hash has invalid n parameter %q %w", n, err)
108 }
109 if memoryPower == 0 {
110 return nil, fmt.Errorf("crypto: Firebase scrypt hash has invalid n=0")
111 }
112 rounds, err := strconv.ParseUint(r, 10, 64)
113 if err != nil {
114 return nil, fmt.Errorf("crypto: Firebase scrypt hash has invalid r parameter %q: %w", r, err)
115 }
116 if rounds == 0 {
117 return nil, fmt.Errorf("crypto: Firebase scrypt hash has invalid r=0")
118 }
119
120 threads, err := strconv.ParseUint(p, 10, 8)
121 if err != nil {
122 return nil, fmt.Errorf("crypto: Firebase scrypt hash has invalid p parameter %q %w", p, err)
123 }
124 if threads == 0 {
125 return nil, fmt.Errorf("crypto: Firebase scrypt hash has invalid p=0")
126 }
127
128 rawHash, err := base64.StdEncoding.DecodeString(hashB64)
129 if err != nil {
130 return nil, fmt.Errorf("crypto: Firebase scrypt hash has invalid base64 in the hash section %w", err)
131 }
132
133 salt, err := base64.StdEncoding.DecodeString(saltB64)
134 if err != nil {
135 return nil, fmt.Errorf("crypto: Firebase scrypt salt has invalid base64 in the hash section %w", err)
136 }
137
138 var saltSeparator, signerKey []byte
139 if signerKey, err = base64.StdEncoding.DecodeString(sk); err != nil {
140 return nil, err
141 }
142 if saltSeparator, err = base64.StdEncoding.DecodeString(ss); err != nil {
143 return nil, err
144 }

Callers 2

NewUserWithPasswordHashFunction · 0.92

Calls

no outgoing calls

Tested by

no test coverage detected