Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

coprocessor: migrate password function from TiDB #7777

Merged
merged 5 commits into from May 14, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions components/tidb_query_normal_expr/src/builtin_encryption.rs
Expand Up @@ -151,6 +151,25 @@ impl ScalarFunc {
}
Ok(Some(Cow::Owned(gen_random_bytes(length as usize))))
}

#[inline]
pub fn password<'a, 'b: 'a>(
&'b self,
ctx: &mut EvalContext,
row: &[Datum],
) -> Result<Option<Cow<'a, [u8]>>> {
let pass = try_opt!(self.children[0].eval_string(ctx, row));
if pass.len() == 0 {
return Ok(Some(Cow::Owned(vec![])));
}
ctx.warnings.append_warning(Error::Other(box_err!(
"Warning: Deprecated syntax PASSWORD"
)));
let hash1 = hex_digest(MessageDigest::sha1(), &pass)?;
let mut hash2 = hex_digest(MessageDigest::sha1(), &hash1)?;
hash2.insert(0, b'*');
Ok(Some(Cow::Owned(hash2)))
}
}

#[inline]
Expand Down Expand Up @@ -384,4 +403,25 @@ mod tests {
Datum::Null
);
}

#[test]
fn test_password() {
let cases = vec![
("TiKV", "*cca644408381f962dba8dfb9889db1371ee74208"),
("Pingcap", "*f33bc75eac70ac317621fbbfa560d6251c43cf8a"),
("rust", "*090c2b08e0c1776910e777b917c2185be6554c2e"),
("database", "*02e86b4af5219d0ba6c974908aea62d42eb7da24"),
("raft", "*b23a77787ed44e62ef2570f03ce8982d119fb699"),
];
let mut ctx = EvalContext::default();

for (input_str, exp_str) in cases {
let input = datum_expr(Datum::Bytes(input_str.as_bytes().to_vec()));
let op = scalar_func_expr(ScalarFuncSig::Password, &[input]);
let op = Expression::build(&mut ctx, op).unwrap();
let got = op.eval(&mut ctx, &[]).unwrap();
let exp = Datum::Bytes(exp_str.as_bytes().to_vec());
assert_eq!(got, exp, "password('{:?}')", input_str);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be better to add testing for empty input and the warning I think :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Fullstop000 I have added a Null case test, similar to the above tests. PTAL.

}
}
}
2 changes: 2 additions & 0 deletions components/tidb_query_normal_expr/src/scalar_function.rs
Expand Up @@ -296,6 +296,7 @@ impl ScalarFunc {
| ScalarFuncSig::Degrees
| ScalarFuncSig::Sha1
| ScalarFuncSig::Md5
| ScalarFuncSig::Password
| ScalarFuncSig::Radians
| ScalarFuncSig::Exp
| ScalarFuncSig::Trim1Arg
Expand Down Expand Up @@ -897,6 +898,7 @@ dispatch_call! {
Uuid => uuid,
Sha1 => sha1,
Sha2 => sha2,
Password => password,
Elt => elt,
FromBase64 => from_base64,
ToBase64 => to_base64,
Expand Down