File tree 2 files changed +17
-5
lines changed
2 files changed +17
-5
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package uuid // import "github.com/xtls/xray-core/common/uuid"
3
3
import (
4
4
"bytes"
5
5
"crypto/rand"
6
+ "crypto/sha1"
6
7
"encoding/hex"
7
8
8
9
"github.com/xtls/xray-core/common"
@@ -67,8 +68,18 @@ func ParseString(str string) (UUID, error) {
67
68
var uuid UUID
68
69
69
70
text := []byte (str )
70
- if len (text ) < 32 {
71
- return uuid , errors .New ("invalid UUID: " , str )
71
+ if l := len (text ); l < 32 || l > 36 {
72
+ if l == 0 || l > 30 {
73
+ return uuid , errors .New ("invalid UUID: " , str )
74
+ }
75
+ h := sha1 .New ()
76
+ h .Write (uuid [:])
77
+ h .Write (text )
78
+ u := h .Sum (nil )[:16 ]
79
+ u [6 ] = (u [6 ] & 0x0f ) | (5 << 4 )
80
+ u [8 ] = (u [8 ]& (0xff >> 2 ) | (0x02 << 6 ))
81
+ copy (uuid [:], u )
82
+ return uuid , nil
72
83
}
73
84
74
85
b := uuid .Bytes ()
Original file line number Diff line number Diff line change @@ -35,9 +35,10 @@ func TestParseString(t *testing.T) {
35
35
t .Fatal (r )
36
36
}
37
37
38
- _ , err = ParseString ("2418d087" )
39
- if err == nil {
40
- t .Fatal ("Expect error but nil" )
38
+ u0 , _ := ParseString ("example" )
39
+ u5 , _ := ParseString ("feb54431-301b-52bb-a6dd-e1e93e81bb9e" )
40
+ if r := cmp .Diff (u0 , u5 ); r != "" {
41
+ t .Fatal (r )
41
42
}
42
43
43
44
_ , err = ParseString ("2418d087-648k-4990-86e8-19dca1d006d3" )
You can’t perform that action at this time.
0 commit comments