Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ShortenedDigestTest.java « test « crypto « bouncycastle « org « java « test « src « core - gitlab.com/quite/humla-spongycastle.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 20510c941e9808c19591f6595844cf7cf698d4cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * Created on 6/05/2006
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package org.bouncycastle.crypto.test;

import org.bouncycastle.crypto.ExtendedDigest;
import org.bouncycastle.crypto.digests.SHA1Digest;
import org.bouncycastle.crypto.digests.SHA512Digest;
import org.bouncycastle.crypto.digests.ShortenedDigest;
import org.bouncycastle.util.test.SimpleTest;

public class ShortenedDigestTest
    extends SimpleTest
{
    public void performTest()
    {
        ExtendedDigest  d = new SHA1Digest();
        ShortenedDigest sd = new ShortenedDigest(new SHA1Digest(), 10);
        
        if (sd.getDigestSize() != 10)
        {
            fail("size check wrong for SHA-1");
        }
        
        if (sd.getByteLength() != d.getByteLength())
        {
            fail("byte length check wrong for SHA-1");
        }
        
        //
        // check output fits
        //
        sd.doFinal(new byte[10], 0);
        
        d = new SHA512Digest();
        sd = new ShortenedDigest(new SHA512Digest(), 20);
        
        if (sd.getDigestSize() != 20)
        {
            fail("size check wrong for SHA-512");
        }
        
        if (sd.getByteLength() != d.getByteLength())
        {
            fail("byte length check wrong for SHA-512");
        }
        
        //
        // check output fits
        //
        sd.doFinal(new byte[20], 0);
        
        try
        {
            new ShortenedDigest(null, 20);
            
            fail("null parameter not caught");
        }
        catch (IllegalArgumentException e)
        {
            // expected
        }
        
        try
        {
            new ShortenedDigest(new SHA1Digest(), 50);
            
            fail("short digest not caught");
        }
        catch (IllegalArgumentException e)
        {
            // expected
        }
    }
    
    public String getName()
    {
        return "ShortenedDigest";
    }

    public static void main(
        String[]    args)
    {
        runTest(new ShortenedDigestTest());
    }
}