MoveFingerView.m 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2012-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import <CommonCrypto/CommonDigest.h>
  21. #import "MoveFingerView.h"
  22. @implementation MoveFingerView {
  23. unsigned char digest[CC_SHA256_DIGEST_LENGTH];
  24. }
  25. @synthesize numberOfPositionsRecorded;
  26. - (id)initWithCoder:(NSCoder *)aDecoder {
  27. self = [super initWithCoder:aDecoder];
  28. if (self) {
  29. bzero(digest, sizeof(digest));
  30. self.accessibilityTraits |= UIAccessibilityTraitAllowsDirectInteraction;
  31. }
  32. return self;
  33. }
  34. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  35. for (UITouch *touch in touches)
  36. [self processTouch:touch];
  37. }
  38. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  39. for (UITouch *touch in touches)
  40. [self processTouch:touch];
  41. }
  42. - (void)processTouch:(UITouch*)touch {
  43. CC_SHA256_CTX ctx;
  44. CC_SHA256_Init(&ctx);
  45. /* add last digest */
  46. CC_SHA256_Update(&ctx, digest, sizeof(digest));
  47. /* add position and time stamp of this touch */
  48. CGPoint location = [touch locationInView:nil];
  49. NSTimeInterval timestamp = touch.timestamp;
  50. CC_SHA256_Update(&ctx, &location, sizeof(location));
  51. CC_SHA256_Update(&ctx, &timestamp, sizeof(timestamp));
  52. CC_SHA256_Final(digest, &ctx);
  53. numberOfPositionsRecorded++;
  54. [self.delegate didMoveFingerInView:self];
  55. }
  56. - (NSData *)digest {
  57. return [NSData dataWithBytes:digest length:sizeof(digest)];
  58. }
  59. @end