Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use core;
- use core::prelude::*;
- use collections;
- pub struct ObfuscatedString {
- crypted: &'static [u8],
- key: &'static [u8]
- }
- impl ObfuscatedString {
- pub fn dec_into(&self, dest: &mut [u8]) {
- let &ObfuscatedString { crypted, key } = self;
- for (d, (c, k)) in dest.iter_mut().zip(crypted.iter().zip(key.iter())) {
- *d = *c ^ *k;
- }
- }
- pub fn dec_vec(&self) -> collections::vec::Vec<u8> {
- let len = self.crypted.len();
- let mut v = collections::vec::Vec::with_capacity(len);
- unsafe {
- // safe because of with_capacity
- v.set_len(len);
- }
- self.dec_into(v.as_mut_slice());
- v
- }
- pub fn dec(&self) -> collections::string::String {
- // this is safe, because all constructors of ObfuscatedString
- // are unsafe; thus the invariant that decryption produces valid UTF-8
- // is maintained in safe code
- unsafe {
- collections::string::raw::from_utf8(self.dec_vec())
- }
- }
- }
- impl core::cmp::PartialEq<str> for ObfuscatedString {
- fn eq(&self, other: &str) -> bool {
- let decrypted = self.crypted.iter().zip(self.key.iter()).map(|(&a, &b)| a ^ b);
- core::iter::order::eq(decrypted, other.bytes())
- }
- }
- impl core::cmp::PartialEq<ObfuscatedString> for str {
- fn eq(&self, other: &ObfuscatedString) -> bool {
- other == self
- }
- }
- impl<'a> core::cmp::PartialEq<ObfuscatedString> for &'a str {
- fn eq(&self, other: &ObfuscatedString) -> bool {
- other == *self
- }
- }
- pub unsafe fn construct(crypted: &'static [u8], key: &'static [u8]) -> ObfuscatedString {
- ObfuscatedString {
- crypted: crypted,
- key: key
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement