应用程序引擎中的 key_name 应用的规则是什么?
我正在尝试使用应用程序引擎 User 对象的 user_id (由 User.user_id() 方法返回)作为我自己的 User 类中的 key_name 。问题是它一直告诉我这是一个无效的 key_name。我尝试过对其进行sha2'ing,并使用digest()以及hexdigest()方法来减少可能的字符数,但仍然没有好的结果。这是因为值太长,还是因为键名不能包含某些字符?另外,如何修改 user_id 使其保持唯一,但也可用作实体的 key_name ?如果它使用哈希值,那么 user_id 就无法被猜测,那就额外的好处了。
这是发生错误的代码:
def get_current_user():
return User.get(db.Key(hashlib.sha1(users.get_current_user().user_id()).hexdigest()))
我现在正在做一些更多的测试,考虑评论和答案中的建议。
I'm trying to use an app engine User object's user_id (returned by the User.user_id() method) as a key_name in my own User class. The problem is that it keeps telling me that it's an invalid key_name. I've tried sha2'ing it, and using the digest() as well as the hexdigest() method to reduce the number of possible characters, but still no good result. Is this because the value is too long, or because key names can't have certain characters? And also, how can I modify a user_id in such a way that it stays unique, but is also usable as a key_name for an entity? Extra bonus if it uses a hash so that thje user_id can't be guessed.
Here is the code where the error occured:
def get_current_user():
return User.get(db.Key(hashlib.sha1(users.get_current_user().user_id()).hexdigest()))
I'm now doing some more testing, concidering suggestions from the comments and answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定为什么它不适合你,当我在开发控制台中运行它时,以下内容没有问题。
但是,如果您正在对其进行散列(听起来可能是这样),请注意您可能会发生冲突。如果您将密钥提供给客户端,我会避免使用哈希,并会考虑其他一些匿名化方法。例如,您可以放弃其密钥的另一个模型,其中存储了用户的密钥。另一种方法是加密
id
(对所有用户使用相同的密钥)而不是对其进行散列。如果您正在执行生成二进制数据(加密/哈希摘要)的操作,则应用程序引擎(至少是 SDK)存在问题,因此您需要先对其进行编码,然后将其用作 key_name。
I'm not sure why it isn't working for you, the following has no issues when I run it in the dev console.
However if you are hashing it (which it sounds like you may be), be aware that you may get a collision. I would avoid against using a hash and would consider some other means of anonymization if you are giving the key to clients. Such as another model whose key you can give away, that has the user's key stored in it. Another method would be to encrypt the
id
(using the same key for all users) rather than hash it.If you are doing something that generates binary data (encryption / hash digest) app engine (the sdk at-least) has issues, so you need to encode it first, and use that as the key_name.