Skip to content

Commit f172ce3

Browse files
committed
• Converted SSYDigester to use CommonCrypto instead of openssl. Tests completed and passed.
• Replaced deprecated method in SSYHintArrow.m.
1 parent 219774a commit f172ce3

File tree

3 files changed

+48
-24
lines changed

3 files changed

+48
-24
lines changed

SSYDigester.h

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#import <Cocoa/Cocoa.h>
2-
#include <openssl/evp.h>
2+
#import <CommonCrypto/CommonDigest.h>
33

44
enum SSYDigesterAlgorithm_enum {
55
SSYDigesterAlgorithmMd5,
@@ -13,7 +13,9 @@ typedef enum SSYDigesterAlgorithm_enum SSYDigesterAlgorithm ;
1313
@details
1414
*/
1515
@interface SSYDigester : NSObject {
16-
EVP_MD_CTX m_context ;
16+
SSYDigesterAlgorithm m_algorithm ;
17+
CC_MD5_CTX m_context_md5 ;
18+
CC_SHA1_CTX m_context_sha1 ;
1719
}
1820

1921
/*!
@@ -63,6 +65,9 @@ typedef enum SSYDigesterAlgorithm_enum SSYDigesterAlgorithm ;
6365
@end
6466

6567
#if 0
68+
/* The following method was used to test this class on 20150304, when it was
69+
converted to use CommonCrypto instead of openssl. The "expected" results
70+
were obtained using the old openssl code. */
6671
+ (void)testSSYDigester {
6772
SSYDigester* digester ;
6873
NSData* data ;
@@ -140,7 +145,6 @@ typedef enum SSYDigesterAlgorithm_enum SSYDigesterAlgorithm ;
140145
length:20] ;
141146
NSLog(@"sha1 expect = %@", expected) ;
142147
NSLog(@"sha1 test %@", [data isEqual:expected] ? @"passed" : @"failed") ;
143-
144148
}
145149

146150
#endif

SSYDigester.m

+28-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#import "SSYDigester.h"
2-
#include <openssl/err.h>
32

43
NSString* const msgSSYDigesterContextIsDefunct = @"Context is defunct." ;
54

@@ -10,10 +9,12 @@ - (id)initWithAlgorithm:(SSYDigesterAlgorithm)algorithm {
109
if (self) {
1110
switch (algorithm) {
1211
case SSYDigesterAlgorithmMd5:
13-
EVP_DigestInit(&m_context, EVP_md5()) ;
12+
m_algorithm = SSYDigesterAlgorithmMd5 ;
13+
CC_MD5_Init(&m_context_md5) ;
1414
break;
15-
case SSYDigesterAlgorithmSha1:
16-
EVP_DigestInit(&m_context, EVP_sha1()) ;
15+
case SSYDigesterAlgorithmSha1:;
16+
m_algorithm = SSYDigesterAlgorithmSha1 ;
17+
CC_SHA1_Init(&m_context_sha1) ;
1718
break;
1819
}
1920
}
@@ -22,7 +23,14 @@ - (id)initWithAlgorithm:(SSYDigesterAlgorithm)algorithm {
2223
}
2324

2425
- (void)updateWithData:(NSData*)data {
25-
EVP_DigestUpdate(&m_context, [data bytes], [data length]) ;
26+
switch (m_algorithm) {
27+
case SSYDigesterAlgorithmMd5:
28+
CC_MD5_Update(&m_context_md5, [data bytes], (CC_LONG)[data length]) ;
29+
break ;
30+
case SSYDigesterAlgorithmSha1:
31+
CC_SHA1_Update(&m_context_sha1, [data bytes], (CC_LONG)[data length]) ;
32+
break ;
33+
}
2634
}
2735

2836

@@ -59,17 +67,24 @@ - (void)updateWithString:(NSString*)string
5967

6068
NSData* data = [NSData dataWithBytes:cString
6169
length:length] ;
62-
EVP_DigestUpdate(&m_context, [data bytes], [data length]) ;
70+
[self updateWithData:data] ;
6371
}
6472

6573
- (NSData*)finalizeDigest {
66-
unsigned int length ;
67-
unsigned char value[EVP_MAX_MD_SIZE] ;
68-
69-
EVP_DigestFinal(&m_context, value, &length) ;
70-
EVP_MD_CTX_cleanup(&m_context) ;
71-
return [NSData dataWithBytes:value
72-
length:length];
74+
NSMutableData* hash ;
75+
switch (m_algorithm) {
76+
case SSYDigesterAlgorithmMd5:;
77+
hash = [[NSMutableData alloc] initWithLength:CC_MD5_DIGEST_LENGTH] ;
78+
CC_MD5_Final([hash mutableBytes], &m_context_md5) ;
79+
break ;
80+
case SSYDigesterAlgorithmSha1:;
81+
hash = [[NSMutableData alloc] initWithLength:CC_SHA1_DIGEST_LENGTH] ;
82+
CC_SHA1_Final([hash mutableBytes], &m_context_sha1) ;
83+
break ;
84+
}
85+
NSData* answer = [NSData dataWithData:hash] ;
86+
[hash release] ;
87+
return answer ;
7388
}
7489

7590
@end

SSYHintArrow.m

+13-8
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,34 @@
99
#define SSYHINTARROW_TOP_COLOR [NSColor colorWithCalibratedRed:(80.0/256.0) green:(111.0/256.0) blue:(246.0/256.0) alpha:1.0]
1010
#define SSYHINTARROW_BOTTOM_COLOR [NSColor colorWithCalibratedRed:(27.0/256.0) green:(68.0/256.0) blue:(243.0/256.0) alpha:1.0]
1111
#define SSYHINTARROW_BORDER_COLOR [NSColor whiteColor]
12-
#define SSYHINTARROW_SCALE_FACTOR [[NSScreen mainScreen] userSpaceScaleFactor]
1312

1413
static SSYHintArrow* static_helpArrow = nil ;
1514

1615
@implementation SSYHintArrow
1716

1817
# pragma mark * Heavy Lifters
1918

19+
- (CGFloat)scaleFactor {
20+
// Was using deprecated [NSScreen mainScreen] userSpaceScaleFactor]
21+
CGFloat factor = [[self contentView] backingScaleFactor] ;
22+
return factor ;
23+
}
24+
2025
- (void)updateGeometry {
2126
[m_view setFrame:NSMakeRect(0.0, 0.0, m_size.width, m_size.height)] ;
2227

2328
NSRect contentRect = NSZeroRect ;
2429
contentRect.size = [m_view frame].size ;
2530

2631
// Account for viewMargin.
27-
m_viewFrame = NSMakeRect(m_viewMargin * SSYHINTARROW_SCALE_FACTOR,
28-
m_viewMargin * SSYHINTARROW_SCALE_FACTOR,
32+
m_viewFrame = NSMakeRect(m_viewMargin * [self scaleFactor],
33+
m_viewMargin * [self scaleFactor],
2934
[m_view frame].size.width, [m_view frame].size.height) ;
3035
contentRect = NSInsetRect(contentRect,
31-
-m_viewMargin * SSYHINTARROW_SCALE_FACTOR,
32-
-m_viewMargin * SSYHINTARROW_SCALE_FACTOR) ;
36+
-m_viewMargin * [self scaleFactor],
37+
-m_viewMargin * [self scaleFactor]) ;
3338

34-
CGFloat scaledArrowHeight = m_arrowHeight * SSYHINTARROW_SCALE_FACTOR ;
39+
CGFloat scaledArrowHeight = m_arrowHeight * [self scaleFactor] ;
3540
m_viewFrame.origin.x += scaledArrowHeight ;
3641
contentRect.size.width += scaledArrowHeight ;
3742

@@ -56,7 +61,7 @@ - (void)updateGeometry {
5661

5762

5863
- (NSBezierPath *)backgroundPath {
59-
CGFloat scaleFactor = SSYHINTARROW_SCALE_FACTOR ;
64+
CGFloat scaleFactor = [self scaleFactor] ;
6065
CGFloat scaledRadius = m_cornerRadius * scaleFactor ;
6166
NSRect contentArea = NSInsetRect(m_viewFrame,
6267
-m_viewMargin * scaleFactor,
@@ -122,7 +127,7 @@ - (NSColor *)backgroundColorPatternImage {
122127
// Draw border if appropriate.
123128
if (m_borderWidth > 0) {
124129
// Double the borderWidth since we're drawing inside the path.
125-
[bgPath setLineWidth:(m_borderWidth * 2.0) * SSYHINTARROW_SCALE_FACTOR] ;
130+
[bgPath setLineWidth:(m_borderWidth * 2.0) * [self scaleFactor]] ;
126131
[m_borderColor set] ;
127132
[bgPath stroke] ;
128133
}

0 commit comments

Comments
 (0)