Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
002cf5fa
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
002cf5fa
编写于
7月 15, 2014
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'rm-uuid'
上级
f3c1832a
19fae86f
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
78 addition
and
78 deletion
+78
-78
activerecord/lib/active_record/fixtures.rb
activerecord/lib/active_record/fixtures.rb
+2
-2
activesupport/CHANGELOG.md
activesupport/CHANGELOG.md
+1
-1
activesupport/lib/active_support/core_ext/digest/uuid.rb
activesupport/lib/active_support/core_ext/digest/uuid.rb
+51
-0
activesupport/lib/active_support/core_ext/securerandom.rb
activesupport/lib/active_support/core_ext/securerandom.rb
+0
-47
activesupport/test/core_ext/digest/uuid_test.rb
activesupport/test/core_ext/digest/uuid_test.rb
+24
-0
activesupport/test/core_ext/securerandom_test.rb
activesupport/test/core_ext/securerandom_test.rb
+0
-28
未找到文件。
activerecord/lib/active_record/fixtures.rb
浏览文件 @
002cf5fa
...
...
@@ -2,7 +2,7 @@
require
'yaml'
require
'zlib'
require
'active_support/dependencies'
require
'active_support/core_ext/
securerandom
'
require
'active_support/core_ext/
digest/uuid
'
require
'active_record/fixture_set/file'
require
'active_record/errors'
...
...
@@ -551,7 +551,7 @@ def self.create_fixtures(fixtures_directory, fixture_set_names, class_names = {}
# Integer identifiers are values less than 2^30. UUIDs are RFC 4122 version 5 SHA-1 hashes.
def
self
.
identify
(
label
,
column_type
=
:integer
)
if
column_type
==
:uuid
SecureRandom
.
uuid_v5
(
SecureRandom
::
UUID_
OID_NAMESPACE
,
label
.
to_s
)
Digest
::
UUID
.
uuid_v5
(
Digest
::
UUID
::
OID_NAMESPACE
,
label
.
to_s
)
else
Zlib
.
crc32
(
label
.
to_s
)
%
MAX_ID
end
...
...
activesupport/CHANGELOG.md
浏览文件 @
002cf5fa
...
...
@@ -160,7 +160,7 @@
*Bogdan Gusiev*
*
Add
`
SecureRandom::uuid_v3`
and
`SecureRandom
::uuid_v5`
to support stable
*
Add
`
Digest::UUID::uuid_v3`
and
`Digest::UUID
::uuid_v5`
to support stable
UUID fixtures on PostgreSQL.
*Roderick van Domburg*
...
...
activesupport/lib/active_support/core_ext/digest/uuid.rb
0 → 100644
浏览文件 @
002cf5fa
require
'securerandom'
module
Digest
module
UUID
DNS_NAMESPACE
=
"k
\xA7\xB8\x10\x9D\xAD\x11\xD1\x80\xB4\x00\xC0
O
\xD4
0
\xC8
"
#:nodoc:
URL_NAMESPACE
=
"k
\xA7\xB8\x11\x9D\xAD\x11\xD1\x80\xB4\x00\xC0
O
\xD4
0
\xC8
"
#:nodoc:
OID_NAMESPACE
=
"k
\xA7\xB8\x12\x9D\xAD\x11\xD1\x80\xB4\x00\xC0
O
\xD4
0
\xC8
"
#:nodoc:
X500_NAMESPACE
=
"k
\xA7\xB8\x14\x9D\xAD\x11\xD1\x80\xB4\x00\xC0
O
\xD4
0
\xC8
"
#:nodoc:
# Generates a v5 non-random UUID (Universally Unique IDentifier).
#
# Using Digest::MD5 generates version 3 UUIDs; Digest::SHA1 generates version 5 UUIDs.
# uuid_from_hash always generates the same UUID for a given name and namespace combination.
#
# See RFC 4122 for details of UUID at: http://www.ietf.org/rfc/rfc4122.txt
def
self
.
uuid_from_hash
(
hash_class
,
uuid_namespace
,
name
)
if
hash_class
==
Digest
::
MD5
version
=
3
elsif
hash_class
==
Digest
::
SHA1
version
=
5
else
raise
ArgumentError
,
"Expected Digest::SHA1 or Digest::MD5, got
#{
hash_class
.
name
}
."
end
hash
=
hash_class
.
new
hash
.
update
(
uuid_namespace
)
hash
.
update
(
name
)
ary
=
hash
.
digest
.
unpack
(
'NnnnnN'
)
ary
[
2
]
=
(
ary
[
2
]
&
0x0FFF
)
|
(
version
<<
12
)
ary
[
3
]
=
(
ary
[
3
]
&
0x3FFF
)
|
0x8000
"%08x-%04x-%04x-%04x-%04x%08x"
%
ary
end
# Convenience method for uuid_from_hash using Digest::MD5.
def
self
.
uuid_v3
(
uuid_namespace
,
name
)
self
.
uuid_from_hash
(
Digest
::
MD5
,
uuid_namespace
,
name
)
end
# Convenience method for uuid_from_hash using Digest::SHA1.
def
self
.
uuid_v5
(
uuid_namespace
,
name
)
self
.
uuid_from_hash
(
Digest
::
SHA1
,
uuid_namespace
,
name
)
end
# Convenience method for SecureRandom.uuid.
def
self
.
uuid_v4
SecureRandom
.
uuid
end
end
end
activesupport/lib/active_support/core_ext/securerandom.rb
已删除
100644 → 0
浏览文件 @
f3c1832a
module
SecureRandom
#:nodoc:
UUID_DNS_NAMESPACE
=
"k
\xA7\xB8\x10\x9D\xAD\x11\xD1\x80\xB4\x00\xC0
O
\xD4
0
\xC8
"
#:nodoc:
UUID_URL_NAMESPACE
=
"k
\xA7\xB8\x11\x9D\xAD\x11\xD1\x80\xB4\x00\xC0
O
\xD4
0
\xC8
"
#:nodoc:
UUID_OID_NAMESPACE
=
"k
\xA7\xB8\x12\x9D\xAD\x11\xD1\x80\xB4\x00\xC0
O
\xD4
0
\xC8
"
#:nodoc:
UUID_X500_NAMESPACE
=
"k
\xA7\xB8\x14\x9D\xAD\x11\xD1\x80\xB4\x00\xC0
O
\xD4
0
\xC8
"
#:nodoc:
# Generates a v5 non-random UUID (Universally Unique IDentifier).
#
# Using Digest::MD5 generates version 3 UUIDs; Digest::SHA1 generates version 5 UUIDs.
# ::uuid_from_hash always generates the same UUID for a given name and namespace combination.
#
# See RFC 4122 for details of UUID at: http://www.ietf.org/rfc/rfc4122.txt
def
self
.
uuid_from_hash
(
hash_class
,
uuid_namespace
,
name
)
#:nodoc:
if
hash_class
==
Digest
::
MD5
version
=
3
elsif
hash_class
==
Digest
::
SHA1
version
=
5
else
raise
ArgumentError
,
"Expected Digest::SHA1 or Digest::MD5, got
#{
hash_class
.
name
}
."
end
hash
=
hash_class
.
new
hash
.
update
(
uuid_namespace
)
hash
.
update
(
name
)
ary
=
hash
.
digest
.
unpack
(
'NnnnnN'
)
ary
[
2
]
=
(
ary
[
2
]
&
0x0FFF
)
|
(
version
<<
12
)
ary
[
3
]
=
(
ary
[
3
]
&
0x3FFF
)
|
0x8000
"%08x-%04x-%04x-%04x-%04x%08x"
%
ary
end
# Convenience method for ::uuid_from_hash using Digest::MD5.
def
self
.
uuid_v3
(
uuid_namespace
,
name
)
#:nodoc:
self
.
uuid_from_hash
(
Digest
::
MD5
,
uuid_namespace
,
name
)
end
# Convenience method for ::uuid_from_hash using Digest::SHA1.
def
self
.
uuid_v5
(
uuid_namespace
,
name
)
#:nodoc:
self
.
uuid_from_hash
(
Digest
::
SHA1
,
uuid_namespace
,
name
)
end
class
<<
self
# Alias for ::uuid.
alias_method
:uuid_v4
,
:uuid
end
end
activesupport/test/core_ext/digest/uuid_test.rb
0 → 100644
浏览文件 @
002cf5fa
require
'abstract_unit'
require
'active_support/core_ext/digest/uuid'
class
DigestUUIDExt
<
ActiveSupport
::
TestCase
def
test_v3_uuids
assert_equal
"3d813cbb-47fb-32ba-91df-831e1593ac29"
,
Digest
::
UUID
.
uuid_v3
(
Digest
::
UUID
::
DNS_NAMESPACE
,
"www.widgets.com"
)
assert_equal
"86df55fb-428e-3843-8583-ba3c05f290bc"
,
Digest
::
UUID
.
uuid_v3
(
Digest
::
UUID
::
URL_NAMESPACE
,
"http://www.widgets.com"
)
assert_equal
"8c29ab0e-a2dc-3482-b5eb-20cb2e2387a1"
,
Digest
::
UUID
.
uuid_v3
(
Digest
::
UUID
::
OID_NAMESPACE
,
"1.2.3"
)
assert_equal
"ee49149d-53a4-304a-890b-468229f6afc3"
,
Digest
::
UUID
.
uuid_v3
(
Digest
::
UUID
::
X500_NAMESPACE
,
"cn=John Doe, ou=People, o=Acme, Inc., c=US"
)
end
def
test_v5_uuids
assert_equal
"21f7f8de-8051-5b89-8680-0195ef798b6a"
,
Digest
::
UUID
.
uuid_v5
(
Digest
::
UUID
::
DNS_NAMESPACE
,
"www.widgets.com"
)
assert_equal
"4e570fd8-186d-5a74-90f0-4d28e34673a1"
,
Digest
::
UUID
.
uuid_v5
(
Digest
::
UUID
::
URL_NAMESPACE
,
"http://www.widgets.com"
)
assert_equal
"42d5e23b-3a02-5135-85c6-52d1102f1f00"
,
Digest
::
UUID
.
uuid_v5
(
Digest
::
UUID
::
OID_NAMESPACE
,
"1.2.3"
)
assert_equal
"fd5b2ddf-bcfe-58b6-90d6-db50f74db527"
,
Digest
::
UUID
.
uuid_v5
(
Digest
::
UUID
::
X500_NAMESPACE
,
"cn=John Doe, ou=People, o=Acme, Inc., c=US"
)
end
def
test_invalid_hash_class
assert_raise
ArgumentError
do
Digest
::
UUID
.
uuid_from_hash
(
Digest
::
SHA2
,
Digest
::
UUID
::
OID_NAMESPACE
,
'1.2.3'
)
end
end
end
activesupport/test/core_ext/securerandom_test.rb
已删除
100644 → 0
浏览文件 @
f3c1832a
require
'abstract_unit'
require
'active_support/core_ext/securerandom'
class
SecureRandomExt
<
ActiveSupport
::
TestCase
def
test_v3_uuids
assert_equal
"3d813cbb-47fb-32ba-91df-831e1593ac29"
,
SecureRandom
.
uuid_v3
(
SecureRandom
::
UUID_DNS_NAMESPACE
,
"www.widgets.com"
)
assert_equal
"86df55fb-428e-3843-8583-ba3c05f290bc"
,
SecureRandom
.
uuid_v3
(
SecureRandom
::
UUID_URL_NAMESPACE
,
"http://www.widgets.com"
)
assert_equal
"8c29ab0e-a2dc-3482-b5eb-20cb2e2387a1"
,
SecureRandom
.
uuid_v3
(
SecureRandom
::
UUID_OID_NAMESPACE
,
"1.2.3"
)
assert_equal
"ee49149d-53a4-304a-890b-468229f6afc3"
,
SecureRandom
.
uuid_v3
(
SecureRandom
::
UUID_X500_NAMESPACE
,
"cn=John Doe, ou=People, o=Acme, Inc., c=US"
)
end
def
test_v5_uuids
assert_equal
"21f7f8de-8051-5b89-8680-0195ef798b6a"
,
SecureRandom
.
uuid_v5
(
SecureRandom
::
UUID_DNS_NAMESPACE
,
"www.widgets.com"
)
assert_equal
"4e570fd8-186d-5a74-90f0-4d28e34673a1"
,
SecureRandom
.
uuid_v5
(
SecureRandom
::
UUID_URL_NAMESPACE
,
"http://www.widgets.com"
)
assert_equal
"42d5e23b-3a02-5135-85c6-52d1102f1f00"
,
SecureRandom
.
uuid_v5
(
SecureRandom
::
UUID_OID_NAMESPACE
,
"1.2.3"
)
assert_equal
"fd5b2ddf-bcfe-58b6-90d6-db50f74db527"
,
SecureRandom
.
uuid_v5
(
SecureRandom
::
UUID_X500_NAMESPACE
,
"cn=John Doe, ou=People, o=Acme, Inc., c=US"
)
end
def
test_uuid_v4_alias
assert_equal
SecureRandom
.
method
(
:uuid_v4
),
SecureRandom
.
method
(
:uuid
)
end
def
test_invalid_hash_class
assert_raise
ArgumentError
do
SecureRandom
.
uuid_from_hash
(
Digest
::
SHA2
,
SecureRandom
::
UUID_OID_NAMESPACE
,
'1.2.3'
)
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录